Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ Performance improvements
- Performance improvement in :func:`factorize` for object columns not containing strings (:issue:`51921`)
- Performance improvement in :class:`Series` reductions (:issue:`52341`)
- Performance improvement in :meth:`Series.to_numpy` when dtype is a numpy float dtype and ``na_value`` is ``np.nan`` (:issue:`52430`)
- Performance improvement in :meth:`Series.corr` and :meth:`Series.cov` for extension dtypes (:issue:`52502`)
-

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2701,8 +2701,8 @@ def corr(
if len(this) == 0:
return np.nan

this_values = np.asarray(this._values)
other_values = np.asarray(other._values)
this_values = this.to_numpy(dtype=float, na_value=np.nan, copy=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any chance this gets weird if we have non-numeric?

is this the right thing to do if we have NAs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any chance this gets weird if we have non-numeric?

This raises for non-numeric that cannot be cast to float (same as behavior as main branch):

ser = pd.Series(["a", "b"])
ser.corr(ser)

# ValueError: could not convert string to float: 'a'

It allows non-numeric that can be cast to float (same behavior as main branch):

ser = pd.Series(["1", "2"])
ser.corr(ser)

# 0.9999999999999999

is this the right thing to do if we have NAs?

NAs get dropped either way in the underlying nanops.py functions nancorr and nancov.

FYI, this is the same approach used in DataFrame.corr:

mat = data.to_numpy(dtype=float, na_value=np.nan, copy=False)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this is the same approach used in DataFrame.corr:

good point. works for me

other_values = other.to_numpy(dtype=float, na_value=np.nan, copy=False)

if method in ["pearson", "spearman", "kendall"] or callable(method):
return nanops.nancorr(
Expand Down Expand Up @@ -2759,8 +2759,8 @@ def cov(
this, other = self.align(other, join="inner", copy=False)
if len(this) == 0:
return np.nan
this_values = np.asarray(this._values)
other_values = np.asarray(other._values)
this_values = this.to_numpy(dtype=float, na_value=np.nan, copy=False)
other_values = other.to_numpy(dtype=float, na_value=np.nan, copy=False)
return nanops.nancov(
this_values, other_values, min_periods=min_periods, ddof=ddof
)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/methods/test_cov_corr.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ def test_corrwith_mixed_dtypes(self, numeric_only):
tm.assert_series_equal(result, expected)
else:
with pytest.raises(
TypeError,
match=r"Could not convert \['a' 'b' 'c' 'd'\] to numeric",
ValueError,
match="could not convert string to float",
):
df.corrwith(s, numeric_only=numeric_only)

Expand Down