Skip to content

BUG: Fix #25959 - Don't call .array in DatetimeLikeArrayMixin's map #25964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 19, 2019
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: 4 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import warnings

import numpy as np

import pandas as pd
from pandas._config import get_option

from pandas._libs import iNaT, index as libindex, lib, tslibs
Expand Down Expand Up @@ -3687,7 +3687,9 @@ 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 use pd.array instead of tolist
# so extension arrays can be used
return DataFrame(pd.array(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)