diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 72c40b04a1195..d186fdfe0f322 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -124,7 +124,7 @@ Bug Fixes ~~~~~~~~~ - Bug in :func:`to_datetime` which would raise an (incorrect) ``ValueError`` when called with a date far into the future and the ``format`` argument specified instead of raising ``OutOfBoundsDatetime`` (:issue:`23830`) - Bug in an error message in :meth:`DataFrame.plot`. Improved the error message if non-numerics are passed to :meth:`DataFrame.plot` (:issue:`25481`) -- +- Bug in error messages in :meth:`DataFrame.corr` and :meth:`Series.corr`. Added the possibility of using a callable. (:issue:`25729`) Categorical ^^^^^^^^^^^ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 3996728a1cc90..7317682d95256 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -7088,8 +7088,8 @@ def corr(self, method='pearson', min_periods=1): correl[j, i] = c else: raise ValueError("method must be either 'pearson', " - "'spearman', or 'kendall', '{method}' " - "was supplied".format(method=method)) + "'spearman', 'kendall', or a callable, " + "'{method}' was supplied".format(method=method)) return self._constructor(correl, index=idx, columns=cols) diff --git a/pandas/core/series.py b/pandas/core/series.py index 03fc26efa4516..04fe3b4407149 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2159,8 +2159,8 @@ def corr(self, other, method='pearson', min_periods=None): min_periods=min_periods) raise ValueError("method must be either 'pearson', " - "'spearman', or 'kendall', '{method}' " - "was supplied".format(method=method)) + "'spearman', 'kendall', or a callable, " + "'{method}' was supplied".format(method=method)) def cov(self, other, min_periods=None): """ diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 2969e8be2db03..88c8d89ec4b63 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -332,8 +332,8 @@ def test_corr_cov_independent_index_column(self): def test_corr_invalid_method(self): # GH 22298 df = pd.DataFrame(np.random.normal(size=(10, 2))) - msg = ("method must be either 'pearson', 'spearman', " - "or 'kendall'") + msg = ("method must be either 'pearson', " + "'spearman', 'kendall', or a callable, ") with pytest.raises(ValueError, match=msg): df.corr(method="____") diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index d7d9c526503cb..13195a0d81d9c 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -387,8 +387,8 @@ def test_corr_invalid_method(self): # GH PR #22298 s1 = pd.Series(np.random.randn(10)) s2 = pd.Series(np.random.randn(10)) - msg = ("method must be either 'pearson', 'spearman', " - "or 'kendall'") + msg = ("method must be either 'pearson', " + "'spearman', 'kendall', or a callable, ") with pytest.raises(ValueError, match=msg): s1.corr(s2, method="____")