Skip to content

Commit daa23d6

Browse files
authored
TST/CLN: test_cov_corr (#41886)
1 parent 618cc0e commit daa23d6

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

pandas/tests/frame/methods/test_cov_corr.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@ def test_cov(self, float_frame, float_string_frame):
2929
frame = float_frame.copy()
3030
frame["A"][:5] = np.nan
3131
frame["B"][5:10] = np.nan
32-
result = float_frame.cov(min_periods=len(float_frame) - 8)
33-
expected = float_frame.cov()
32+
result = frame.cov(min_periods=len(frame) - 8)
33+
expected = frame.cov()
3434
expected.loc["A", "B"] = np.nan
3535
expected.loc["B", "A"] = np.nan
36+
tm.assert_frame_equal(result, expected)
3637

3738
# regular
38-
float_frame["A"][:5] = np.nan
39-
float_frame["B"][:10] = np.nan
40-
cov = float_frame.cov()
41-
42-
tm.assert_almost_equal(cov["A"]["C"], float_frame["A"].cov(float_frame["C"]))
39+
result = frame.cov()
40+
expected = frame["A"].cov(frame["C"])
41+
tm.assert_almost_equal(result["A"]["C"], expected)
4342

4443
# exclude non-numeric types
4544
result = float_string_frame.cov()
@@ -101,10 +100,7 @@ def test_corr_scipy_method(self, float_frame, method):
101100
# ---------------------------------------------------------------------
102101

103102
@td.skip_if_no_scipy
104-
def test_corr_non_numeric(self, float_frame, float_string_frame):
105-
float_frame["A"][:5] = np.nan
106-
float_frame["B"][5:10] = np.nan
107-
103+
def test_corr_non_numeric(self, float_string_frame):
108104
# exclude non-numeric types
109105
result = float_string_frame.corr()
110106
expected = float_string_frame.loc[:, ["A", "B", "C", "D"]].corr()
@@ -143,27 +139,27 @@ def test_corr_constant(self, meth):
143139
assert isna(rs.values).all()
144140

145141
@td.skip_if_no_scipy
146-
def test_corr_int_and_boolean(self):
142+
@pytest.mark.parametrize("meth", ["pearson", "kendall", "spearman"])
143+
def test_corr_int_and_boolean(self, meth):
147144
# when dtypes of pandas series are different
148145
# then ndarray will have dtype=object,
149146
# so it need to be properly handled
150147
df = DataFrame({"a": [True, False], "b": [1, 0]})
151148

152149
expected = DataFrame(np.ones((2, 2)), index=["a", "b"], columns=["a", "b"])
153-
for meth in ["pearson", "kendall", "spearman"]:
154150

155-
with warnings.catch_warnings(record=True):
156-
warnings.simplefilter("ignore", RuntimeWarning)
157-
result = df.corr(meth)
158-
tm.assert_frame_equal(result, expected)
151+
with warnings.catch_warnings(record=True):
152+
warnings.simplefilter("ignore", RuntimeWarning)
153+
result = df.corr(meth)
154+
tm.assert_frame_equal(result, expected)
159155

160-
def test_corr_cov_independent_index_column(self):
156+
@pytest.mark.parametrize("method", ["cov", "corr"])
157+
def test_corr_cov_independent_index_column(self, method):
161158
# GH#14617
162159
df = DataFrame(np.random.randn(4 * 10).reshape(10, 4), columns=list("abcd"))
163-
for method in ["cov", "corr"]:
164-
result = getattr(df, method)()
165-
assert result.index is not result.columns
166-
assert result.index.equals(result.columns)
160+
result = getattr(df, method)()
161+
assert result.index is not result.columns
162+
assert result.index.equals(result.columns)
167163

168164
def test_corr_invalid_method(self):
169165
# GH#22298
@@ -174,10 +170,10 @@ def test_corr_invalid_method(self):
174170

175171
def test_corr_int(self):
176172
# dtypes other than float64 GH#1761
177-
df3 = DataFrame({"a": [1, 2, 3, 4], "b": [1, 2, 3, 4]})
173+
df = DataFrame({"a": [1, 2, 3, 4], "b": [1, 2, 3, 4]})
178174

179-
df3.cov()
180-
df3.corr()
175+
df.cov()
176+
df.corr()
181177

182178
@td.skip_if_no_scipy
183179
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)