|
| 1 | +from itertools import combinations |
1 | 2 | import numpy as np |
2 | 3 | import pytest |
3 | 4 |
|
@@ -251,6 +252,24 @@ def test_corr_numeric_only(self, meth, numeric_only): |
251 | 252 | else: |
252 | 253 | with pytest.raises(ValueError, match="could not convert string to float"): |
253 | 254 | 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 | + |
254 | 273 |
|
255 | 274 |
|
256 | 275 | class TestDataFrameCorrWith: |
|
0 commit comments