diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index f2500bb29d0be..805f57eabbd17 100644 --- a/doc/source/whatsnew/v0.22.0.txt +++ b/doc/source/whatsnew/v0.22.0.txt @@ -77,6 +77,7 @@ Other Enhancements - :func:`Series.fillna` now accepts a Series or a dict as a ``value`` for a categorical dtype (:issue:`17033`) - :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`) - Improved wording of ``ValueError`` raised in :func:`read_csv` when the ``usecols`` argument cannot match all columns. (:issue:`17301`) +- :func:`DataFrame.corrwith` now silently drops non-numeric columns when passed a Series. Before, an exception was raised (:issue:`18570`). .. _whatsnew_0220.api_breaking: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 313c9ec872179..68cf5dd7161e3 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5577,7 +5577,7 @@ def corrwith(self, other, axis=0, drop=False): Parameters ---------- - other : DataFrame + other : DataFrame, Series axis : {0 or 'index', 1 or 'columns'}, default 0 0 or 'index' to compute column-wise, 1 or 'columns' for row-wise drop : boolean, default False @@ -5588,10 +5588,11 @@ def corrwith(self, other, axis=0, drop=False): correls : Series """ axis = self._get_axis_number(axis) + this = self._get_numeric_data() + if isinstance(other, Series): - return self.apply(other.corr, axis=axis) + return this.apply(other.corr, axis=axis) - this = self._get_numeric_data() other = other._get_numeric_data() left, right = this.align(other, join='inner', copy=False) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index cfdb18cefee64..9ef526034eaf7 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -240,6 +240,16 @@ def test_corrwith_matches_corrcoef(self): tm.assert_almost_equal(c1, c2) assert c1 < 1 + def test_corrwith_mixed_dtypes(self): + # GH 18570 + df = pd.DataFrame({'a': [1, 4, 3, 2], 'b': [4, 6, 7, 3], + 'c': ['a', 'b', 'c', 'd']}) + s = pd.Series([0, 6, 7, 3]) + result = df.corrwith(s) + corrs = [df['a'].corr(s), df['b'].corr(s)] + expected = pd.Series(data=corrs, index=['a', 'b']) + tm.assert_series_equal(result, expected) + def test_bool_describe_in_mixed_frame(self): df = DataFrame({ 'string_data': ['a', 'b', 'c', 'd', 'e'],