From da51ff065779b825436e9f61963b09343dc53143 Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Sun, 18 Sep 2022 15:54:25 +0800 Subject: [PATCH 1/2] Add finalize to df.corr and df.cov --- doc/source/whatsnew/v1.6.0.rst | 5 +++++ pandas/core/frame.py | 6 ++++-- pandas/tests/generic/test_finalize.py | 2 -- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index 405b8cc0a5ded..ab73731904138 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -222,6 +222,11 @@ Styler - - +Metadata +^^^^^^^^ +- Fixed metadata propagation in :meth:`DataFrame.corr` and :meth:`DataFrame.cov` (:issue:`28283`) +- + Other ^^^^^ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 22ccd1d763769..f8bf95031d819 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -10355,7 +10355,8 @@ def corr( f"'{method}' was supplied" ) - return self._constructor(correl, index=idx, columns=cols) + result = self._constructor(correl, index=idx, columns=cols) + return result.__finalize__(self, method="corr") def cov( self, @@ -10490,7 +10491,8 @@ def cov( else: base_cov = libalgos.nancorr(mat, cov=True, minp=min_periods) - return self._constructor(base_cov, index=idx, columns=cols) + result = self._constructor(base_cov, index=idx, columns=cols) + return result.__finalize__(self, method="cov") def corrwith( self, diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index dddab05af7341..eb6909105b055 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -197,12 +197,10 @@ ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("corr")), - marks=not_implemented_mark, ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("cov")), marks=[ - not_implemented_mark, pytest.mark.filterwarnings("ignore::RuntimeWarning"), ], ), From e35b5fe3aa1eaf85f1e59de8eb5eec5e08ab7bd0 Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Tue, 20 Sep 2022 10:21:56 +0800 Subject: [PATCH 2/2] Clean --- pandas/tests/generic/test_finalize.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index eb6909105b055..f1661b60f50b7 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -195,9 +195,7 @@ pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("round", 2)), ), - pytest.param( - (pd.DataFrame, frame_data, operator.methodcaller("corr")), - ), + (pd.DataFrame, frame_data, operator.methodcaller("corr")), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("cov")), marks=[