From 7b0e246bc0fe363bd32c6c735ac964a83aed62bb Mon Sep 17 00:00:00 2001 From: jiawei-zhang-a <2992758051@qq.com> Date: Wed, 26 Apr 2023 05:30:47 +0000 Subject: [PATCH 1/4] Metadata in DataFrame.meann --- pandas/core/frame.py | 7 ++++++- pandas/tests/generic/test_finalize.py | 1 - 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 838a34adeaf82..feef2cf496bdd 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11026,7 +11026,12 @@ def mean( numeric_only: bool = False, **kwargs, ): - return super().mean(axis, skipna, numeric_only, **kwargs) + result = super().mean(axis, skipna, numeric_only, **kwargs) + return ( + result.__finalize__(self, method="mean") + if hasattr(result, "__finalize__") + else result + ) @doc(make_doc("median", ndim=2)) def median( diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index a76b6b94d719d..0db255dccd4dc 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -422,7 +422,6 @@ ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("mean")), - marks=not_implemented_mark, ), ] From 58b46e2e9dc838e40f43d742736445ec4551ea77 Mon Sep 17 00:00:00 2001 From: jiawei-zhang-a <2992758051@qq.com> Date: Wed, 26 Apr 2023 17:23:57 +0000 Subject: [PATCH 2/4] more specifically to skip for type np.float64 --- pandas/core/frame.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index feef2cf496bdd..073772a01d9b2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11027,11 +11027,11 @@ def mean( **kwargs, ): result = super().mean(axis, skipna, numeric_only, **kwargs) - return ( - result.__finalize__(self, method="mean") - if hasattr(result, "__finalize__") - else result - ) + + if isinstance(result, np.float64): + return result + else: + return result.__finalize__(self, method="mean") @doc(make_doc("median", ndim=2)) def median( From e842f9a8442d0eb954e5d395771d5d5f3570131f Mon Sep 17 00:00:00 2001 From: jiawei-zhang-a <2992758051@qq.com> Date: Wed, 26 Apr 2023 21:32:20 +0000 Subject: [PATCH 3/4] only propogate when it is Dataframe of Series --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 073772a01d9b2..2dbac10b00634 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11028,10 +11028,10 @@ def mean( ): result = super().mean(axis, skipna, numeric_only, **kwargs) - if isinstance(result, np.float64): - return result - else: + if isinstance(result, (DataFrame, Series)): return result.__finalize__(self, method="mean") + else: + return result @doc(make_doc("median", ndim=2)) def median( From 312940b79fa882c3d78a977f764f7b91cb83b16f Mon Sep 17 00:00:00 2001 From: root <2992758051@qq.com> Date: Fri, 28 Apr 2023 23:25:42 +0000 Subject: [PATCH 4/4] change the method of finalize --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2dbac10b00634..3092b5cbbc3cb 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11029,7 +11029,7 @@ def mean( result = super().mean(axis, skipna, numeric_only, **kwargs) if isinstance(result, (DataFrame, Series)): - return result.__finalize__(self, method="mean") + return result.__finalize__(self) else: return result