Skip to content

Commit

Permalink
BUG: Fix apply on series backed by datetime aware values (pandas-dev#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasKluiters committed Apr 10, 2019
1 parent 00c119c commit 2803f11
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ Reshaping
- 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`)
- Bug in :func:`concat` where the resulting ``freq`` of two :class:`DatetimeIndex` with the same ``freq`` would be dropped (:issue:`3232`).
- Bug in :func:`merge` where merging with equivalent Categorical dtypes was raising an error (:issue:`22501`)
- Bug in :func:`Series.apply` failed when the series is a timezone aware :class:`DatetimeIndex` (:issue:`25959`)

Sparse
^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3687,7 +3687,11 @@ def f(x):

if len(mapped) and isinstance(mapped[0], Series):
from pandas.core.frame import DataFrame
return DataFrame(mapped.tolist(), index=self.index)
# GH 25959 we don't need to call tolist
# if we've been given an ExtensionArray
if not is_extension_array_dtype(mapped.dtype):
mapped = mapped.tolist()
return DataFrame(mapped, index=self.index)
else:
return self._constructor(mapped,
index=self.index).__finalize__(self)
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,16 @@ def test_map_missing_mixed(self, vals, mapping, exp):
result = s.map(mapping)

tm.assert_series_equal(result, pd.Series(exp))

@pytest.mark.parametrize("dti,exp", [
(Series([1, 2], index=pd.DatetimeIndex([0, 31536000000])),
DataFrame(np.repeat([[1, 2]], 2, axis=0), dtype='int64')),
(tm.makeTimeSeries(nper=30),
DataFrame(np.repeat([[1, 2]], 30, axis=0), dtype='int64'))
])
def test_apply_on_date_time_index_aware_series(self, dti, exp):
# GH 25959
# Calling apply on a localized time series should not cause an error
index = dti.tz_localize('UTC').index
result = pd.Series(index).apply(lambda x: pd.Series([1, 2]))
assert_frame_equal(result, exp)

0 comments on commit 2803f11

Please sign in to comment.