Skip to content

Commit 6cf3b5c

Browse files
ThomasKluitersjreback
authored andcommitted
BUG: Fix pandas-dev#25959 - Don't call .array in DatetimeLikeArrayMixin's map (pandas-dev#25964)
1 parent 420c127 commit 6cf3b5c

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ Reshaping
460460
- Bug in :func:`merge` where merging with equivalent Categorical dtypes was raising an error (:issue:`22501`)
461461
- bug in :class:`DataFrame` instantiating with a ``range`` (e.g. ``pd.DataFrame(range(3))``) raised an error (:issue:`26342`).
462462
- Bug in :class:`DataFrame` constructor when passing non-empty tuples would cause a segmentation fault (:issue:`25691`)
463+
- Bug in :func:`Series.apply` failed when the series is a timezone aware :class:`DatetimeIndex` (:issue:`25959`)
463464
- Bug in :func:`pandas.cut` where large bins could incorrectly raise an error due to an integer overflow (:issue:`26045`)
464465
- Bug in :func:`DataFrame.sort_index` where an error is thrown when a multi-indexed DataFrame is sorted on all levels with the initial level sorted last (:issue:`26053`)
465466
- Bug in :meth:`Series.nlargest` treats ``True`` as smaller than ``False`` (:issue:`26154`)

pandas/core/series.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from pandas.core.dtypes.missing import (
2929
isna, na_value_for_dtype, notna, remove_na_arraylike)
3030

31+
import pandas as pd
3132
from pandas.core import algorithms, base, generic, nanops, ops
3233
from pandas.core.accessor import CachedAccessor
3334
from pandas.core.arrays import ExtensionArray, SparseArray
@@ -3702,8 +3703,10 @@ def f(x):
37023703
mapped = lib.map_infer(values, f, convert=convert_dtype)
37033704

37043705
if len(mapped) and isinstance(mapped[0], Series):
3705-
from pandas.core.frame import DataFrame
3706-
return DataFrame(mapped.tolist(), index=self.index)
3706+
# GH 25959 use pd.array instead of tolist
3707+
# so extension arrays can be used
3708+
return self._constructor_expanddim(pd.array(mapped),
3709+
index=self.index)
37073710
else:
37083711
return self._constructor(mapped,
37093712
index=self.index).__finalize__(self)

pandas/tests/series/test_apply.py

+20
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,23 @@ def test_map_missing_mixed(self, vals, mapping, exp):
671671
result = s.map(mapping)
672672

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

0 commit comments

Comments
 (0)