@@ -186,75 +186,58 @@ def test_corr_callable_method(self, datetime_series):
186186 tm .assert_almost_equal (df .transpose ().corr (method = my_corr ), expected )
187187
188188 @pytest .mark .parametrize ("method" , ["kendall" , "spearman" ])
189+ @pytest .mark .parametrize (
190+ "ord_cat_series" ,
191+ [
192+ Series ( # ordered categorical series
193+ pd .Categorical (
194+ ["low" , "med" , "high" , "very_high" ],
195+ categories = ["low" , "med" , "high" , "very_high" ],
196+ ordered = True ,
197+ )
198+ ),
199+ Series ( # ordered categorical series with nan and a different ranking
200+ pd .Categorical (
201+ ["h" , "low" , "vh" , None ],
202+ categories = ["low" , "m" , "h" , "vh" ],
203+ ordered = True ,
204+ )
205+ ),
206+ ],
207+ )
208+ @pytest .mark .parametrize (
209+ "other_series" ,
210+ [
211+ Series ( # int series against which tord cat series is correlated
212+ [0 , 1 , 2 , 3 ]
213+ ),
214+ Series ( # float series against which ord cat series is correlated
215+ [2.0 , 3.0 , 4.5 , 6.5 ]
216+ ),
217+ Series ( # other ord cat series against which ord cat series is correlated
218+ pd .Categorical (
219+ ["high" , "low" , "very_high" , "med" ],
220+ categories = ["low" , "med" , "high" , "very_high" ],
221+ ordered = True ,
222+ )
223+ ),
224+ ],
225+ )
189226 def test_corr_rank_ordered_categorical (
190227 self ,
191228 method ,
229+ ord_cat_series ,
230+ other_series ,
192231 ):
193232 stats = pytest .importorskip ("scipy.stats" )
194233 method_scipy_func = {"kendall" : stats .kendalltau , "spearman" : stats .spearmanr }
195- ser_ord_cat = Series (
196- pd .Categorical (
197- ["low" , "med" , "high" , "very_high" ],
198- categories = ["low" , "med" , "high" , "very_high" ],
199- ordered = True ,
200- )
201- )
202- ser_ord_cat_codes = ser_ord_cat .cat .codes .replace (- 1 , np .nan )
203- ser_ord_int = Series ([0 , 1 , 2 , 3 ])
204- ser_ord_float = Series ([2.0 , 3.0 , 4.5 , 6.5 ])
205-
206- corr_calc = ser_ord_cat .corr (ser_ord_int , method = method )
207- corr_expected = method_scipy_func [method ](
208- ser_ord_cat_codes , ser_ord_int , nan_policy = "omit"
209- )[0 ]
210- tm .assert_almost_equal (corr_calc , corr_expected )
211-
212- corr_calc = ser_ord_cat .corr (ser_ord_float , method = method )
213- corr_expected = method_scipy_func [method ](
214- ser_ord_cat_codes , ser_ord_float , nan_policy = "omit"
215- )[0 ]
216- tm .assert_almost_equal (corr_calc , corr_expected )
217-
218- corr_calc = ser_ord_cat .corr (ser_ord_cat , method = method )
219- corr_expected = method_scipy_func [method ](
220- ser_ord_cat_codes , ser_ord_cat_codes , nan_policy = "omit"
221- )[0 ]
222- tm .assert_almost_equal (corr_calc , corr_expected )
223-
224- ser_ord_cat_shuff = Series (
225- pd .Categorical (
226- ["high" , "low" , "very_high" , "med" ],
227- categories = ["low" , "med" , "high" , "very_high" ],
228- ordered = True ,
229- )
230- )
231- ser_ord_cat_shuff_codes = ser_ord_cat_shuff .cat .codes .replace (- 1 , np .nan )
232-
233- corr_calc = ser_ord_cat_shuff .corr (ser_ord_cat , method = method )
234- corr_expected = method_scipy_func [method ](
235- ser_ord_cat_shuff_codes , ser_ord_cat_codes , nan_policy = "omit"
236- )[0 ]
237- tm .assert_almost_equal (corr_calc , corr_expected )
234+ ord_ser_cat_codes = ord_cat_series .cat .codes .replace (- 1 , np .nan )
238235
239- corr_calc = ser_ord_cat_shuff .corr (ser_ord_cat_shuff , method = method )
240- corr_expected = method_scipy_func [method ](
241- ser_ord_cat_shuff_codes , ser_ord_cat_shuff_codes , nan_policy = "omit"
242- )[0 ]
243- tm .assert_almost_equal (corr_calc , corr_expected )
236+ if other_series .dtype == "category" and other_series .cat .ordered :
237+ other_series = other_series .cat .codes .replace (- 1 , np .nan )
244238
245- ser_ord_cat_with_nan = Series (
246- pd .Categorical (
247- ["h" , "low" , "vh" , None , "m" ],
248- categories = ["low" , "m" , "h" , "vh" ],
249- ordered = True ,
250- )
251- )
252- ser_ord_cat_shuff_with_nan_codes = ser_ord_cat_with_nan .cat .codes .replace (
253- - 1 , np .nan
254- )
255- ser_ord_int = Series ([2 , 0 , 1 , 3 , None ])
256- corr_calc = ser_ord_cat_with_nan .corr (ser_ord_int , method = method )
239+ corr_calc = ord_cat_series .corr (other_series , method = method )
257240 corr_expected = method_scipy_func [method ](
258- ser_ord_cat_shuff_with_nan_codes , ser_ord_int , nan_policy = "omit"
241+ ord_ser_cat_codes , other_series , nan_policy = "omit"
259242 )[0 ]
260243 tm .assert_almost_equal (corr_calc , corr_expected )
0 commit comments