From 3112a5a534efc4af5488f518a362ebb305b7dbad Mon Sep 17 00:00:00 2001 From: Mohamad Rkein Date: Sun, 17 Oct 2021 18:16:30 +0000 Subject: [PATCH 1/2] Fixed metadata propagation in Dataframe.apply Co-authored-by: Mohamad Rkein Co-authored-by: Rafael Rodrigues --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/core/frame.py | 2 +- pandas/tests/generic/test_finalize.py | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 964f4b83866c9..a02a271bb52c6 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -571,6 +571,7 @@ Reshaping - Bug in :func:`concat` would fail when the ``objs`` argument all had the same index and the ``keys`` argument contained duplicates (:issue:`43595`) - 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`) +- Fixed metadata propagation in :class:`Dataframe.apply` method (:issue:`28283`) Sparse ^^^^^^ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d360c450194f1..2242c7cbd6639 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8866,7 +8866,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..8854d923a793a 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -472,7 +472,6 @@ (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")), From 526fc59dbc9aa31689225e94c5db9564182e7c7e Mon Sep 17 00:00:00 2001 From: Mohamad Rkein Date: Sun, 24 Oct 2021 18:06:36 +0000 Subject: [PATCH 2/2] TST: Remove xfail mark from tests on functions that call DataFrame.apply() --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/tests/generic/test_finalize.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index a02a271bb52c6..38f7432c98313 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -571,7 +571,7 @@ Reshaping - Bug in :func:`concat` would fail when the ``objs`` argument all had the same index and the ``keys`` argument contained duplicates (:issue:`43595`) - 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`) -- Fixed metadata propagation in :class:`Dataframe.apply` method (:issue:`28283`) +- 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/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index 8854d923a793a..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,7 +469,6 @@ 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(