Skip to content

Commit 223f59f

Browse files
BUG: Fix apply on series backed by datetime aware values (#25959)
1 parent 00c119c commit 223f59f

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ Reshaping
387387
- Bug in :func:`concat` where order of ``OrderedDict`` (and ``dict`` in Python 3.6+) is not respected, when passed in as ``objs`` argument (:issue:`21510`)
388388
- Bug in :func:`concat` where the resulting ``freq`` of two :class:`DatetimeIndex` with the same ``freq`` would be dropped (:issue:`3232`).
389389
- Bug in :func:`merge` where merging with equivalent Categorical dtypes was raising an error (:issue:`22501`)
390+
- Bug in :func:`Series.apply` failed when the series is a timezone aware :class:`DatetimeIndex` (:issue:`25959`)
390391

391392
Sparse
392393
^^^^^^

pandas/core/series.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import warnings
88

99
import numpy as np
10-
10+
import pandas as pd
1111
from pandas._config import get_option
1212

1313
from pandas._libs import iNaT, index as libindex, lib, tslibs
@@ -3687,7 +3687,9 @@ def f(x):
36873687

36883688
if len(mapped) and isinstance(mapped[0], Series):
36893689
from pandas.core.frame import DataFrame
3690-
return DataFrame(mapped.tolist(), index=self.index)
3690+
# GH 25959 use pd.array instead of tolist
3691+
# so extension arrays can be used
3692+
return DataFrame(pd.array(mapped), index=self.index)
36913693
else:
36923694
return self._constructor(mapped,
36933695
index=self.index).__finalize__(self)

pandas/tests/series/test_apply.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,16 @@ def test_map_missing_mixed(self, vals, mapping, exp):
677677
result = s.map(mapping)
678678

679679
tm.assert_series_equal(result, pd.Series(exp))
680+
681+
@pytest.mark.parametrize("dti,exp", [
682+
(Series([1, 2], index=pd.DatetimeIndex([0, 31536000000])),
683+
DataFrame(np.repeat([[1, 2]], 2, axis=0), dtype='int64')),
684+
(tm.makeTimeSeries(nper=30),
685+
DataFrame(np.repeat([[1, 2]], 30, axis=0), dtype='int64'))
686+
])
687+
def test_apply_on_date_time_index_aware_series(self, dti, exp):
688+
# GH 25959
689+
# Calling apply on a localized time series should not cause an error
690+
index = dti.tz_localize('UTC').index
691+
result = pd.Series(index).apply(lambda x: pd.Series([1, 2]))
692+
assert_frame_equal(result, exp)

0 commit comments

Comments
 (0)