Skip to content

Commit 89e81bf

Browse files
tdpetroujreback
authored andcommitted
selected numeric data before correlation (#18651)
1 parent 9434a5a commit 89e81bf

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

doc/source/whatsnew/v0.22.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Other Enhancements
7777
- :func:`Series.fillna` now accepts a Series or a dict as a ``value`` for a categorical dtype (:issue:`17033`)
7878
- :func:`pandas.read_clipboard` updated to use qtpy, falling back to PyQt5 and then PyQt4, adding compatibility with Python3 and multiple python-qt bindings (:issue:`17722`)
7979
- Improved wording of ``ValueError`` raised in :func:`read_csv` when the ``usecols`` argument cannot match all columns. (:issue:`17301`)
80+
- :func:`DataFrame.corrwith` now silently drops non-numeric columns when passed a Series. Before, an exception was raised (:issue:`18570`).
8081

8182
.. _whatsnew_0220.api_breaking:
8283

pandas/core/frame.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5577,7 +5577,7 @@ def corrwith(self, other, axis=0, drop=False):
55775577
55785578
Parameters
55795579
----------
5580-
other : DataFrame
5580+
other : DataFrame, Series
55815581
axis : {0 or 'index', 1 or 'columns'}, default 0
55825582
0 or 'index' to compute column-wise, 1 or 'columns' for row-wise
55835583
drop : boolean, default False
@@ -5588,10 +5588,11 @@ def corrwith(self, other, axis=0, drop=False):
55885588
correls : Series
55895589
"""
55905590
axis = self._get_axis_number(axis)
5591+
this = self._get_numeric_data()
5592+
55915593
if isinstance(other, Series):
5592-
return self.apply(other.corr, axis=axis)
5594+
return this.apply(other.corr, axis=axis)
55935595

5594-
this = self._get_numeric_data()
55955596
other = other._get_numeric_data()
55965597

55975598
left, right = this.align(other, join='inner', copy=False)

pandas/tests/frame/test_analytics.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ def test_corrwith_matches_corrcoef(self):
240240
tm.assert_almost_equal(c1, c2)
241241
assert c1 < 1
242242

243+
def test_corrwith_mixed_dtypes(self):
244+
# GH 18570
245+
df = pd.DataFrame({'a': [1, 4, 3, 2], 'b': [4, 6, 7, 3],
246+
'c': ['a', 'b', 'c', 'd']})
247+
s = pd.Series([0, 6, 7, 3])
248+
result = df.corrwith(s)
249+
corrs = [df['a'].corr(s), df['b'].corr(s)]
250+
expected = pd.Series(data=corrs, index=['a', 'b'])
251+
tm.assert_series_equal(result, expected)
252+
243253
def test_bool_describe_in_mixed_frame(self):
244254
df = DataFrame({
245255
'string_data': ['a', 'b', 'c', 'd', 'e'],

0 commit comments

Comments
 (0)