Skip to content

Commit 6d2b46d

Browse files
authored
BUG: Series.corr/cov raising with masked dtype (#51422)
1 parent b070d87 commit 6d2b46d

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Timezones
132132

133133
Numeric
134134
^^^^^^^
135-
-
135+
- Bug in :meth:`Series.corr` and :meth:`Series.cov` raising ``AttributeError`` for masked dtypes (:issue:`51422`)
136136
-
137137

138138
Conversion

pandas/core/nanops.py

+6
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,9 @@ def nancorr(
16101610
if len(a) < min_periods:
16111611
return np.nan
16121612

1613+
a = _ensure_numeric(a)
1614+
b = _ensure_numeric(b)
1615+
16131616
f = get_corr_func(method)
16141617
return f(a, b)
16151618

@@ -1668,6 +1671,9 @@ def nancov(
16681671
if len(a) < min_periods:
16691672
return np.nan
16701673

1674+
a = _ensure_numeric(a)
1675+
b = _ensure_numeric(b)
1676+
16711677
return np.cov(a, b, ddof=ddof)[0, 1]
16721678

16731679

pandas/core/series.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -2643,9 +2643,12 @@ def corr(
26432643
if len(this) == 0:
26442644
return np.nan
26452645

2646+
this_values = np.asarray(this._values)
2647+
other_values = np.asarray(other._values)
2648+
26462649
if method in ["pearson", "spearman", "kendall"] or callable(method):
26472650
return nanops.nancorr(
2648-
this.values, other.values, method=method, min_periods=min_periods
2651+
this_values, other_values, method=method, min_periods=min_periods
26492652
)
26502653

26512654
raise ValueError(
@@ -2698,8 +2701,10 @@ def cov(
26982701
this, other = self.align(other, join="inner", copy=False)
26992702
if len(this) == 0:
27002703
return np.nan
2704+
this_values = np.asarray(this._values)
2705+
other_values = np.asarray(other._values)
27012706
return nanops.nancov(
2702-
this.values, other.values, min_periods=min_periods, ddof=ddof
2707+
this_values, other_values, min_periods=min_periods, ddof=ddof
27032708
)
27042709

27052710
@doc(

pandas/tests/frame/methods/test_cov_corr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def test_corrwith_mixed_dtypes(self, numeric_only):
356356
else:
357357
with pytest.raises(
358358
TypeError,
359-
match=r"unsupported operand type\(s\) for /: 'str' and 'int'",
359+
match=r"Could not convert \['a' 'b' 'c' 'd'\] to numeric",
360360
):
361361
df.corrwith(s, numeric_only=numeric_only)
362362

pandas/tests/series/methods/test_cov_corr.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ def test_cov(self, datetime_series):
4242
assert isna(ts1.cov(ts2, min_periods=12))
4343

4444
@pytest.mark.parametrize("test_ddof", [None, 0, 1, 2, 3])
45-
def test_cov_ddof(self, test_ddof):
45+
@pytest.mark.parametrize("dtype", ["float64", "Float64"])
46+
def test_cov_ddof(self, test_ddof, dtype):
4647
# GH#34611
4748
np_array1 = np.random.rand(10)
4849
np_array2 = np.random.rand(10)
4950

50-
s1 = Series(np_array1)
51-
s2 = Series(np_array2)
51+
s1 = Series(np_array1, dtype=dtype)
52+
s2 = Series(np_array2, dtype=dtype)
5253

5354
result = s1.cov(s2, ddof=test_ddof)
5455
expected = np.cov(np_array1, np_array2, ddof=test_ddof)[0][1]
@@ -57,9 +58,12 @@ def test_cov_ddof(self, test_ddof):
5758

5859
class TestSeriesCorr:
5960
@td.skip_if_no_scipy
60-
def test_corr(self, datetime_series):
61+
@pytest.mark.parametrize("dtype", ["float64", "Float64"])
62+
def test_corr(self, datetime_series, dtype):
6163
from scipy import stats
6264

65+
datetime_series = datetime_series.astype(dtype)
66+
6367
# full overlap
6468
tm.assert_almost_equal(datetime_series.corr(datetime_series), 1)
6569

0 commit comments

Comments
 (0)