diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 042c97a0c98b1..7b8a2a853d1a5 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -1136,6 +1136,7 @@ Groupby/resample/rolling - Bug in :meth:`pandas.core.window.Rolling.median` and :meth:`pandas.core.window.Rolling.quantile` where incorrect results are returned with ``closed='left'`` and ``closed='neither'`` (:issue:`26005`) - Improved :class:`pandas.core.window.Rolling`, :class:`pandas.core.window.Window` and :class:`pandas.core.window.EWM` functions to exclude nuisance columns from results instead of raising errors and raise a ``DataError`` only if all columns are nuisance (:issue:`12537`) - Bug in :meth:`pandas.core.window.Rolling.max` and :meth:`pandas.core.window.Rolling.min` where incorrect results are returned with an empty variable window (:issue:`26005`) +- Raise a helpful exception when an unsupported weighted window function is used as an argument of :meth:`pandas.core.window.Window.aggregate` (:issue:`26597`) Reshaping ^^^^^^^^^ diff --git a/pandas/core/base.py b/pandas/core/base.py index 15baf1bed0ecd..9480e2e425f79 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -314,9 +314,16 @@ def _try_aggregate_string_function(self, arg, *args, **kwargs): f = getattr(np, arg, None) if f is not None: - return f(self, *args, **kwargs) + try: + return f(self, *args, **kwargs) + + except (AttributeError, TypeError): + pass - raise ValueError("{arg} is an unknown string function".format(arg=arg)) + raise AttributeError( + "'{arg}' is not a valid function for " + "'{cls}' object".format(arg=arg, cls=type(self).__name__) + ) def _aggregate(self, arg, *args, **kwargs): """ diff --git a/pandas/tests/window/test_window.py b/pandas/tests/window/test_window.py index 2f3b83e172795..3945a8aaa8b87 100644 --- a/pandas/tests/window/test_window.py +++ b/pandas/tests/window/test_window.py @@ -439,6 +439,22 @@ def test_numpy_compat(self, method): with pytest.raises(UnsupportedFunctionCall, match=msg): getattr(w, method)(dtype=np.float64) + @td.skip_if_no_scipy + @pytest.mark.parametrize("arg", ["median", "var", "std", "kurt", "skew"]) + def test_agg_function_support(self, arg): + df = pd.DataFrame({"A": np.arange(5)}) + roll = df.rolling(2, win_type="triang") + + msg = "'{arg}' is not a valid function for " "'Window' object".format(arg=arg) + with pytest.raises(AttributeError, match=msg): + roll.agg(arg) + + with pytest.raises(AttributeError, match=msg): + roll.agg([arg]) + + with pytest.raises(AttributeError, match=msg): + roll.agg({"A": arg}) + class TestRolling(Base): def setup_method(self, method):