Skip to content

Commit e069810

Browse files
testing frame corr
1 parent 583aca6 commit e069810

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

pandas/tests/frame/methods/test_cov_corr.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from itertools import combinations
12
import numpy as np
23
import pytest
34

@@ -251,6 +252,24 @@ def test_corr_numeric_only(self, meth, numeric_only):
251252
else:
252253
with pytest.raises(ValueError, match="could not convert string to float"):
253254
df.corr(meth, numeric_only=numeric_only)
255+
256+
@pytest.mark.parametrize("method", ["kendall", "spearman"])
257+
def test_corr_rank_ordered_categorical(self, method,):
258+
df = DataFrame(
259+
{
260+
"ord_cat": pd.Series(pd.Categorical(["low", "m", "h", "vh"], categories=["low", "m", "h", "vh"], ordered=True)),
261+
"ord_cat_none": pd.Series(pd.Categorical(["low", "m", "h", None], categories=["low", "m", "h"], ordered=True)),
262+
"ord_int": pd.Series([0, 1, 2, 3]),
263+
"ord_float": pd.Series([2.0, 3.0, 4.5, 6.5]),
264+
"ord_float_nan": pd.Series([2.0, 3.0, 4.5, np.nan]),
265+
"ord_cat_shuff": pd.Series(pd.Categorical(["m", "h", "vh", "low"], categories=["low", "m", "h", "vh"], ordered=True)),
266+
}
267+
)
268+
corr_calc = df.corr(method=method)
269+
for col1, col2 in combinations(["ord_cat", "ord_int", "ord_float"], r=2):
270+
expected = df[col1].corr(df[col2], method=method)
271+
tm.assert_almost_equal(corr_calc[col1][col2], expected)
272+
254273

255274

256275
class TestDataFrameCorrWith:

0 commit comments

Comments
 (0)