From 7007ad8f39f1d1e8952d0b961b6c7d9f18840f27 Mon Sep 17 00:00:00 2001 From: Xiao Yuan Date: Wed, 21 Sep 2022 01:43:14 +0800 Subject: [PATCH] BUG: Fix metadata propagation in df.corr and df.cov, GH28283 (#48616) * Add finalize to df.corr and df.cov * Clean --- doc/source/whatsnew/v1.6.0.rst | 5 +++++ pandas/core/frame.py | 6 ++++-- pandas/tests/generic/test_finalize.py | 6 +----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index 72cf6caec0c48..39752e8a9e750 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -258,6 +258,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 edca6918368fa..5468406b2ea21 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -10356,7 +10356,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, @@ -10491,7 +10492,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 af1a76a6263c7..3c40218ef9024 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -192,14 +192,10 @@ pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("round", 2)), ), - pytest.param( - (pd.DataFrame, frame_data, operator.methodcaller("corr")), - marks=not_implemented_mark, - ), + (pd.DataFrame, frame_data, operator.methodcaller("corr")), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("cov")), marks=[ - not_implemented_mark, pytest.mark.filterwarnings("ignore::RuntimeWarning"), ], ),