diff --git a/doc/source/whatsnew/v1.0.1.rst b/doc/source/whatsnew/v1.0.1.rst index be888f1952636..df8cf1932f793 100644 --- a/doc/source/whatsnew/v1.0.1.rst +++ b/doc/source/whatsnew/v1.0.1.rst @@ -22,6 +22,7 @@ Fixed regressions - Fixed regression in ``.groupby()`` aggregations with categorical dtype using Cythonized reduction functions (e.g. ``first``) (:issue:`31450`) - 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`) - Fixed regression in :meth:`DataFrame.groupby` whereby taking the minimum or maximum of a column with period dtype would raise a ``TypeError``. (:issue:`31471`) +- Fixed regression in :meth:`DataFrame.apply` with object dtype and non-reducing function (:issue:`31505`) - Fixed regression in :meth:`to_datetime` when parsing non-nanosecond resolution datetimes (:issue:`31491`) - Fixed regression in :meth:`~DataFrame.to_csv` where specifying an ``na_rep`` might truncate the values written (:issue:`31447`) - Fixed regression in :class:`Categorical` construction with ``numpy.str_`` categories (:issue:`31499`) diff --git a/pandas/_libs/reduction.pyx b/pandas/_libs/reduction.pyx index 89164c527002a..43d253f632f0f 100644 --- a/pandas/_libs/reduction.pyx +++ b/pandas/_libs/reduction.pyx @@ -114,7 +114,8 @@ cdef class Reducer: if self.typ is not None: # In this case, we also have self.index name = labels[i] - cached_typ = self.typ(chunk, index=self.index, name=name) + cached_typ = self.typ( + chunk, index=self.index, name=name, dtype=arr.dtype) # use the cached_typ if possible if cached_typ is not None: diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index e98f74e133ea9..fe6abef97acc4 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -703,6 +703,14 @@ def apply_list(row): ) tm.assert_series_equal(result, expected) + def test_apply_noreduction_tzaware_object(self): + # https://github.com/pandas-dev/pandas/issues/31505 + df = pd.DataFrame({"foo": [pd.Timestamp("2020", tz="UTC")]}, dtype="object") + result = df.apply(lambda x: x) + tm.assert_frame_equal(result, df) + result = df.apply(lambda x: x.copy()) + tm.assert_frame_equal(result, df) + class TestInferOutputShape: # the user has supplied an opaque UDF where