@@ -210,15 +210,11 @@ def test_corr_nullable_integer(self, nullable_column, other_column, method):
210210 @pytest .mark .parametrize ("length" , [2 , 20 , 200 , 2000 ])
211211 def test_corr_for_constant_columns (self , length ):
212212 # GH: 37448
213- # now matches numpy behavior
214213 df = DataFrame (length * [[0.4 , 0.1 ]], columns = ["A" , "B" ])
215214 result = df .corr ()
216- if length == 2 :
217- expected = DataFrame (
218- {"A" : [np .nan , np .nan ], "B" : [np .nan , np .nan ]}, index = ["A" , "B" ]
219- )
220- else :
221- expected = DataFrame ({"A" : [1.0 , 1.0 ], "B" : [1.0 , 1.0 ]}, index = ["A" , "B" ])
215+ expected = DataFrame (
216+ {"A" : [np .nan , np .nan ], "B" : [np .nan , np .nan ]}, index = ["A" , "B" ]
217+ )
222218 tm .assert_frame_equal (result , expected )
223219
224220 def test_calc_corr_small_numbers (self ):
@@ -498,11 +494,50 @@ def test_cov_with_missing_values(self):
498494 tm .assert_frame_equal (result1 , expected )
499495 tm .assert_frame_equal (result2 , expected )
500496
501- def test_close_corr (self ):
502- values = np .array (
503- [[30.0 , 30.100000381469727 ], [116.80000305175781 , 116.8000030517578 ]]
504- )
505- df = DataFrame (values .T )
497+ pair_cases = [
498+ np .array (
499+ [[30.0 , 30.100000381469727 ], [116.80000305175781 , 116.8000030517578 ]],
500+ dtype = np .longdouble ,
501+ ),
502+ np .array (
503+ [[- 30.0 , 30.100000381469727 ], [116.80000305175781 , - 116.8000030517578 ]],
504+ dtype = np .longdouble ,
505+ ),
506+ np .array ([[1e-8 , 3.42e-8 ], [2e-9 , 3e-8 ]], dtype = np .longdouble ),
507+ np .array ([[1e12 , 1e-8 ], [1e12 + 1e-3 , 2e-8 ]], dtype = np .longdouble ),
508+ np .array ([[0.0 , 1e-12 ], [1e-14 , 0.0 ]], dtype = np .longdouble ),
509+ ]
510+
511+ @pytest .mark .parametrize ("values" , pair_cases )
512+ def test_pair_correlation (self , values ):
513+ df = DataFrame (values .T , dtype = np .longdouble )
506514 result = df .corr (method = "pearson" )
507- expected = DataFrame (np .corrcoef (values [0 ], values [1 ]))
515+ expected = DataFrame (np .corrcoef (values [0 ], values [1 ]), dtype = np .float64 )
516+ tm .assert_frame_equal (result , expected )
517+
518+ multi_cases = [
519+ np .array (
520+ [[1e12 , 1e-8 , 5.5 ], [1e12 + 1e-3 , 2e-8 , 5.50000001 ]], dtype = np .longdouble
521+ ),
522+ np .array (
523+ [
524+ [1e12 , 1e12 + 1e-3 , 1e12 + 2e-3 ],
525+ [1e12 + 2e-3 , 1e12 + 3e-3 , 1e12 + 4e-3 ],
526+ [1e12 + 1e-2 , 1e12 + 1e-2 , 1e12 + 1e-2 ],
527+ ],
528+ dtype = np .longdouble ,
529+ ),
530+ np .array ([[1e-8 , 2e-8 ], [2e-8 , 3e-8 ], [0.0 , 1e-12 ]], dtype = np .longdouble ),
531+ ]
532+
533+ @pytest .mark .parametrize ("values" , multi_cases )
534+ def test_multi_correlation (self , values ):
535+ df = DataFrame (values .T , dtype = np .longdouble )
536+ result = df .corr (method = "pearson" )
537+ expected = DataFrame (
538+ np .corrcoef (values ),
539+ index = range (values .shape [0 ]),
540+ columns = range (values .shape [0 ]),
541+ dtype = np .float64 ,
542+ )
508543 tm .assert_frame_equal (result , expected )
0 commit comments