From ac77ff23698edb5ca9a261d7aae29290a9fdf4b8 Mon Sep 17 00:00:00 2001 From: jiawei-zhang-a <2992758051@qq.com> Date: Wed, 26 Apr 2023 05:20:54 +0000 Subject: [PATCH 1/4] Fix the metadata propagation both in Dataframe.std --- pandas/core/frame.py | 3 ++- pandas/tests/generic/test_finalize.py | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 838a34adeaf82..343ec024d9388 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11069,7 +11069,8 @@ def std( numeric_only: bool = False, **kwargs, ): - return super().std(axis, skipna, ddof, numeric_only, **kwargs) + result = super().std(axis, skipna, ddof, numeric_only, **kwargs) + return result.__finalize__(self, method="std") @doc(make_doc("skew", ndim=2)) def skew( diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index a76b6b94d719d..7403ba271663d 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -418,7 +418,6 @@ ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("std")), - marks=not_implemented_mark, ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("mean")), From 1116cdf7815276165c7d5e3fd2054645966eb353 Mon Sep 17 00:00:00 2001 From: jiawei-zhang-a <2992758051@qq.com> Date: Wed, 26 Apr 2023 21:43:28 +0000 Subject: [PATCH 2/4] Fix the typing problem by forcing propogation only on Series --- pandas/core/frame.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 343ec024d9388..73128b401e21a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11070,7 +11070,10 @@ def std( **kwargs, ): result = super().std(axis, skipna, ddof, numeric_only, **kwargs) - return result.__finalize__(self, method="std") + if isinstance(result, Series): + return result.__finalize__(self, method="std") + else: + return result @doc(make_doc("skew", ndim=2)) def skew( From 9d8cb2cb9207203839f61ab5068e8cee783d1da7 Mon Sep 17 00:00:00 2001 From: root <2992758051@qq.com> Date: Fri, 28 Apr 2023 22:52:09 +0000 Subject: [PATCH 3/4] From judge statement to cast --- pandas/core/frame.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 73128b401e21a..1c96f96a30fec 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11069,11 +11069,8 @@ def std( numeric_only: bool = False, **kwargs, ): - result = super().std(axis, skipna, ddof, numeric_only, **kwargs) - if isinstance(result, Series): - return result.__finalize__(self, method="std") - else: - return result + result = cast(Series, super().std(axis, skipna, ddof, numeric_only, **kwargs)) + return result.__finalize__(self, method="std") @doc(make_doc("skew", ndim=2)) def skew( From 2b34bd2f1c2489826652e5a5d14dbb976b2f4ee8 Mon Sep 17 00:00:00 2001 From: root <2992758051@qq.com> Date: Sat, 29 Apr 2023 11:30:48 +0000 Subject: [PATCH 4/4] Add whatsnew --- doc/source/whatsnew/v2.1.0.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index cfce12c2930d7..328d6a5092b36 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -417,6 +417,10 @@ Styler - - +Metadata +^^^^^^^^ +- Fixed metadata propagation in :meth:`DataFrame.std` (:issue:`28283`) + Other ^^^^^ - Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)