From 9c20b5282493bfd16ae2a74d00fcca3119f07b55 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Tue, 8 Jun 2021 23:16:07 -0400 Subject: [PATCH 1/4] TST/CLN: test_cov_corr --- pandas/tests/frame/methods/test_cov_corr.py | 40 +++++++++------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/pandas/tests/frame/methods/test_cov_corr.py b/pandas/tests/frame/methods/test_cov_corr.py index 3da3d82ec77f9..e211c240278a2 100644 --- a/pandas/tests/frame/methods/test_cov_corr.py +++ b/pandas/tests/frame/methods/test_cov_corr.py @@ -29,7 +29,7 @@ def test_cov(self, float_frame, float_string_frame): frame = float_frame.copy() frame["A"][:5] = np.nan frame["B"][5:10] = np.nan - result = float_frame.cov(min_periods=len(float_frame) - 8) + expected = float_frame.cov() expected.loc["A", "B"] = np.nan expected.loc["B", "A"] = np.nan @@ -100,17 +100,12 @@ def test_corr_scipy_method(self, float_frame, method): # --------------------------------------------------------------------- - @td.skip_if_no_scipy - def test_corr_non_numeric(self, float_frame, float_string_frame): - float_frame["A"][:5] = np.nan - float_frame["B"][5:10] = np.nan - + def test_corr_non_numeric(self, float_string_frame): # exclude non-numeric types result = float_string_frame.corr() expected = float_string_frame.loc[:, ["A", "B", "C", "D"]].corr() tm.assert_frame_equal(result, expected) - @td.skip_if_no_scipy @pytest.mark.parametrize("meth", ["pearson", "kendall", "spearman"]) def test_corr_nooverlap(self, meth): # nothing in common @@ -128,7 +123,6 @@ def test_corr_nooverlap(self, meth): assert rs.loc["B", "B"] == 1 assert isna(rs.loc["C", "C"]) - @td.skip_if_no_scipy @pytest.mark.parametrize("meth", ["pearson", "spearman"]) def test_corr_constant(self, meth): # constant --> all NA @@ -142,28 +136,27 @@ def test_corr_constant(self, meth): rs = df.corr(meth) assert isna(rs.values).all() - @td.skip_if_no_scipy - def test_corr_int_and_boolean(self): + @pytest.mark.parametrize("meth", ["pearson", "kendall", "spearman"]) + def test_corr_int_and_boolean(self, meth): # when dtypes of pandas series are different # then ndarray will have dtype=object, # so it need to be properly handled df = DataFrame({"a": [True, False], "b": [1, 0]}) expected = DataFrame(np.ones((2, 2)), index=["a", "b"], columns=["a", "b"]) - for meth in ["pearson", "kendall", "spearman"]: - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", RuntimeWarning) - result = df.corr(meth) - tm.assert_frame_equal(result, expected) + with warnings.catch_warnings(record=True): + warnings.simplefilter("ignore", RuntimeWarning) + result = df.corr(meth) + tm.assert_frame_equal(result, expected) - def test_corr_cov_independent_index_column(self): + @pytest.mark.parametrize("method", ["cov", "corr"]) + def test_corr_cov_independent_index_column(self, method): # GH#14617 df = DataFrame(np.random.randn(4 * 10).reshape(10, 4), columns=list("abcd")) - for method in ["cov", "corr"]: - result = getattr(df, method)() - assert result.index is not result.columns - assert result.index.equals(result.columns) + result = getattr(df, method)() + assert result.index is not result.columns + assert result.index.equals(result.columns) def test_corr_invalid_method(self): # GH#22298 @@ -174,12 +167,11 @@ def test_corr_invalid_method(self): def test_corr_int(self): # dtypes other than float64 GH#1761 - df3 = DataFrame({"a": [1, 2, 3, 4], "b": [1, 2, 3, 4]}) + df = DataFrame({"a": [1, 2, 3, 4], "b": [1, 2, 3, 4]}) - df3.cov() - df3.corr() + df.cov() + df.corr() - @td.skip_if_no_scipy @pytest.mark.parametrize( "nullable_column", [pd.array([1, 2, 3]), pd.array([1, 2, None])] ) From 791e7d7ade872559c863e89b0b1635441274b470 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Wed, 9 Jun 2021 00:09:36 -0400 Subject: [PATCH 2/4] Add back decorators --- pandas/tests/frame/methods/test_cov_corr.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/tests/frame/methods/test_cov_corr.py b/pandas/tests/frame/methods/test_cov_corr.py index e211c240278a2..86ecd3281111a 100644 --- a/pandas/tests/frame/methods/test_cov_corr.py +++ b/pandas/tests/frame/methods/test_cov_corr.py @@ -100,12 +100,14 @@ def test_corr_scipy_method(self, float_frame, method): # --------------------------------------------------------------------- + @td.skip_if_no_scipy def test_corr_non_numeric(self, float_string_frame): # exclude non-numeric types result = float_string_frame.corr() expected = float_string_frame.loc[:, ["A", "B", "C", "D"]].corr() tm.assert_frame_equal(result, expected) + @td.skip_if_no_scipy @pytest.mark.parametrize("meth", ["pearson", "kendall", "spearman"]) def test_corr_nooverlap(self, meth): # nothing in common @@ -123,6 +125,7 @@ def test_corr_nooverlap(self, meth): assert rs.loc["B", "B"] == 1 assert isna(rs.loc["C", "C"]) + @td.skip_if_no_scipy @pytest.mark.parametrize("meth", ["pearson", "spearman"]) def test_corr_constant(self, meth): # constant --> all NA @@ -136,6 +139,7 @@ def test_corr_constant(self, meth): rs = df.corr(meth) assert isna(rs.values).all() + @td.skip_if_no_scipy @pytest.mark.parametrize("meth", ["pearson", "kendall", "spearman"]) def test_corr_int_and_boolean(self, meth): # when dtypes of pandas series are different From 798727a65a7fe4edfe7694c56c597fb05d496af0 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Wed, 9 Jun 2021 00:51:52 -0400 Subject: [PATCH 3/4] Add back last one --- pandas/tests/frame/methods/test_cov_corr.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/frame/methods/test_cov_corr.py b/pandas/tests/frame/methods/test_cov_corr.py index 86ecd3281111a..7416c72e37fed 100644 --- a/pandas/tests/frame/methods/test_cov_corr.py +++ b/pandas/tests/frame/methods/test_cov_corr.py @@ -176,6 +176,7 @@ def test_corr_int(self): df.cov() df.corr() + @td.skip_if_no_scipy @pytest.mark.parametrize( "nullable_column", [pd.array([1, 2, 3]), pd.array([1, 2, None])] ) From 1ebf38d0cfe236746059131ee5d8d11688bd9480 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Wed, 9 Jun 2021 11:12:04 -0400 Subject: [PATCH 4/4] Touch up test_cov test --- pandas/tests/frame/methods/test_cov_corr.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pandas/tests/frame/methods/test_cov_corr.py b/pandas/tests/frame/methods/test_cov_corr.py index 7416c72e37fed..9a67360e9b11a 100644 --- a/pandas/tests/frame/methods/test_cov_corr.py +++ b/pandas/tests/frame/methods/test_cov_corr.py @@ -29,17 +29,16 @@ def test_cov(self, float_frame, float_string_frame): frame = float_frame.copy() frame["A"][:5] = np.nan frame["B"][5:10] = np.nan - - expected = float_frame.cov() + result = frame.cov(min_periods=len(frame) - 8) + expected = frame.cov() expected.loc["A", "B"] = np.nan expected.loc["B", "A"] = np.nan + tm.assert_frame_equal(result, expected) # regular - float_frame["A"][:5] = np.nan - float_frame["B"][:10] = np.nan - cov = float_frame.cov() - - tm.assert_almost_equal(cov["A"]["C"], float_frame["A"].cov(float_frame["C"])) + result = frame.cov() + expected = frame["A"].cov(frame["C"]) + tm.assert_almost_equal(result["A"]["C"], expected) # exclude non-numeric types result = float_string_frame.cov()