diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 254a004a37c40..4d3317b037e01 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -607,6 +607,7 @@ Reshaping - Bug in :func:`concat` which ignored the ``sort`` parameter (:issue:`43375`) - Fixed bug in :func:`merge` with :class:`MultiIndex` as column index for the ``on`` argument returning an error when assigning a column internally (:issue:`43734`) - Bug in :func:`crosstab` would fail when inputs are lists or tuples (:issue:`44076`) +- Fixed metadata propagation in :meth:`Dataframe.apply` method, consequently fixing the same issue for :meth:`Dataframe.transform`, :meth:`Dataframe.nunique` and :meth:`Dataframe.mode` (:issue:`28283`) Sparse ^^^^^^ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5afb19f1d91fe..0224621bf0329 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8873,7 +8873,7 @@ def apply( args=args, kwargs=kwargs, ) - return op.apply() + return op.apply().__finalize__(self, method="apply") def applymap( self, func: PythonFuncType, na_action: str | None = None, **kwargs diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index 50ecb74924e2a..c1f8b5dd7cf41 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -233,7 +233,6 @@ ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("nunique")), - marks=not_implemented_mark, ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("idxmin")), @@ -245,6 +244,9 @@ ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("mode")), + ), + pytest.param( + (pd.Series, [0], operator.methodcaller("mode")), marks=not_implemented_mark, ), pytest.param( @@ -467,12 +469,10 @@ frame_mi_data, operator.methodcaller("transform", lambda x: x - x.min()), ), - marks=not_implemented_mark, ), (pd.Series, ([1],), operator.methodcaller("apply", lambda x: x)), pytest.param( (pd.DataFrame, frame_mi_data, operator.methodcaller("apply", lambda x: x)), - marks=not_implemented_mark, ), # Cumulative reductions (pd.Series, ([1],), operator.methodcaller("cumsum")),