Skip to content

Commit a9d61f0

Browse files
Backport PR pandas-dev#31614: REGR: fix non-reduction apply with tz-aware objects (pandas-dev#31685)
1 parent f55dd11 commit a9d61f0

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v1.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
- Fixed regression in ``.groupby()`` aggregations with categorical dtype using Cythonized reduction functions (e.g. ``first``) (:issue:`31450`)
2323
- Fixed regression in :meth:`GroupBy.apply` if called with a function which returned a non-pandas non-scalar object (e.g. a list or numpy array) (:issue:`31441`)
2424
- Fixed regression in :meth:`DataFrame.groupby` whereby taking the minimum or maximum of a column with period dtype would raise a ``TypeError``. (:issue:`31471`)
25+
- Fixed regression in :meth:`DataFrame.apply` with object dtype and non-reducing function (:issue:`31505`)
2526
- Fixed regression in :meth:`to_datetime` when parsing non-nanosecond resolution datetimes (:issue:`31491`)
2627
- Fixed regression in :meth:`~DataFrame.to_csv` where specifying an ``na_rep`` might truncate the values written (:issue:`31447`)
2728
- Fixed regression in :class:`Categorical` construction with ``numpy.str_`` categories (:issue:`31499`)

pandas/_libs/reduction.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ cdef class Reducer:
114114
if self.typ is not None:
115115
# In this case, we also have self.index
116116
name = labels[i]
117-
cached_typ = self.typ(chunk, index=self.index, name=name)
117+
cached_typ = self.typ(
118+
chunk, index=self.index, name=name, dtype=arr.dtype)
118119

119120
# use the cached_typ if possible
120121
if cached_typ is not None:

pandas/tests/frame/test_apply.py

+8
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,14 @@ def apply_list(row):
703703
)
704704
tm.assert_series_equal(result, expected)
705705

706+
def test_apply_noreduction_tzaware_object(self):
707+
# https://github.com/pandas-dev/pandas/issues/31505
708+
df = pd.DataFrame({"foo": [pd.Timestamp("2020", tz="UTC")]}, dtype="object")
709+
result = df.apply(lambda x: x)
710+
tm.assert_frame_equal(result, df)
711+
result = df.apply(lambda x: x.copy())
712+
tm.assert_frame_equal(result, df)
713+
706714

707715
class TestInferOutputShape:
708716
# the user has supplied an opaque UDF where

0 commit comments

Comments
 (0)