1111)
1212import pandas ._testing as tm
1313
14+
1415class TestSeriesCov :
1516 def test_cov (self , datetime_series ):
1617 # full overlap
@@ -183,54 +184,77 @@ def test_corr_callable_method(self, datetime_series):
183184 df = pd .DataFrame ([s1 , s2 ])
184185 expected = pd .DataFrame ([{0 : 1.0 , 1 : 0 }, {0 : 0 , 1 : 1.0 }])
185186 tm .assert_almost_equal (df .transpose ().corr (method = my_corr ), expected )
186-
187+
187188 @pytest .mark .parametrize ("method" , ["kendall" , "spearman" ])
188- def test_corr_rank_ordered_categorical (self , method ,):
189+ def test_corr_rank_ordered_categorical (
190+ self ,
191+ method ,
192+ ):
189193 stats = pytest .importorskip ("scipy.stats" )
190- method_scipy_func = {
191- "kendall" : stats . kendalltau ,
192- "spearman" : stats . spearmanr
193- }
194- ser_ord_cat = pd . Series ( pd . Categorical (
195- [ "low" , "med" , "high" , "very_high" ],
196- categories = [ "low" , "med" , "high" , "very_high" ], ordered = True
197- ) )
194+ 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+ )
198202 ser_ord_cat_codes = ser_ord_cat .cat .codes .replace (- 1 , np .nan )
199- ser_ord_int = pd . Series ([0 , 1 , 2 , 3 ])
200- ser_ord_float = pd . Series ([2.0 , 3.0 , 4.5 , 6.5 ])
201-
203+ ser_ord_int = Series ([0 , 1 , 2 , 3 ])
204+ ser_ord_float = Series ([2.0 , 3.0 , 4.5 , 6.5 ])
205+
202206 corr_calc = ser_ord_cat .corr (ser_ord_int , method = method )
203- corr_expected = method_scipy_func [method ](ser_ord_cat_codes , ser_ord_int , nan_policy = "omit" )[0 ]
207+ corr_expected = method_scipy_func [method ](
208+ ser_ord_cat_codes , ser_ord_int , nan_policy = "omit"
209+ )[0 ]
204210 tm .assert_almost_equal (corr_calc , corr_expected )
205211
206212 corr_calc = ser_ord_cat .corr (ser_ord_float , method = method )
207- corr_expected = method_scipy_func [method ](ser_ord_cat_codes , ser_ord_float , nan_policy = "omit" )[0 ]
213+ corr_expected = method_scipy_func [method ](
214+ ser_ord_cat_codes , ser_ord_float , nan_policy = "omit"
215+ )[0 ]
208216 tm .assert_almost_equal (corr_calc , corr_expected )
209217
210218 corr_calc = ser_ord_cat .corr (ser_ord_cat , method = method )
211- corr_expected = method_scipy_func [method ](ser_ord_cat_codes , ser_ord_cat_codes , nan_policy = "omit" )[0 ]
219+ corr_expected = method_scipy_func [method ](
220+ ser_ord_cat_codes , ser_ord_cat_codes , nan_policy = "omit"
221+ )[0 ]
212222 tm .assert_almost_equal (corr_calc , corr_expected )
213223
214- ser_ord_cat_shuff = pd .Series ( pd .Categorical (
215- ["high" , "low" , "very_high" , "med" ],
216- categories = ["low" , "med" , "high" , "very_high" ], ordered = True
217- ))
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+ )
218231 ser_ord_cat_shuff_codes = ser_ord_cat_shuff .cat .codes .replace (- 1 , np .nan )
219-
232+
220233 corr_calc = ser_ord_cat_shuff .corr (ser_ord_cat , method = method )
221- corr_expected = method_scipy_func [method ](ser_ord_cat_shuff_codes , ser_ord_cat_codes , nan_policy = "omit" )[0 ]
234+ corr_expected = method_scipy_func [method ](
235+ ser_ord_cat_shuff_codes , ser_ord_cat_codes , nan_policy = "omit"
236+ )[0 ]
222237 tm .assert_almost_equal (corr_calc , corr_expected )
223238
224239 corr_calc = ser_ord_cat_shuff .corr (ser_ord_cat_shuff , method = method )
225- corr_expected = method_scipy_func [method ](ser_ord_cat_shuff_codes , ser_ord_cat_shuff_codes , nan_policy = "omit" )[0 ]
240+ corr_expected = method_scipy_func [method ](
241+ ser_ord_cat_shuff_codes , ser_ord_cat_shuff_codes , nan_policy = "omit"
242+ )[0 ]
226243 tm .assert_almost_equal (corr_calc , corr_expected )
227-
228- ser_ord_cat_with_nan = pd .Series ( pd .Categorical (
229- ["h" , "low" , "vh" , None , "m" ],
230- categories = ["low" , "m" , "h" , "vh" ], ordered = True
231- ))
232- ser_ord_cat_shuff_with_nan_codes = ser_ord_cat_with_nan .cat .codes .replace (- 1 , np .nan )
233- ser_ord_int = pd .Series ([2 , 0 , 1 , 3 , None ])
244+
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 ])
234256 corr_calc = ser_ord_cat_with_nan .corr (ser_ord_int , method = method )
235- corr_expected = method_scipy_func [method ](ser_ord_cat_shuff_with_nan_codes , ser_ord_int , nan_policy = "omit" )[0 ]
236- tm .assert_almost_equal (corr_calc , corr_expected )
257+ corr_expected = method_scipy_func [method ](
258+ ser_ord_cat_shuff_with_nan_codes , ser_ord_int , nan_policy = "omit"
259+ )[0 ]
260+ tm .assert_almost_equal (corr_calc , corr_expected )
0 commit comments