Skip to content

Commit d3e84b7

Browse files
ihsansecerjreback
authored andcommitted
ENH: Raise ValueError for unsupported Window functions (pandas-dev#27275)
1 parent 0cefff0 commit d3e84b7

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ Groupby/resample/rolling
11381138
- 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`)
11391139
- 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`)
11401140
- 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`)
1141+
- Raise a helpful exception when an unsupported weighted window function is used as an argument of :meth:`pandas.core.window.Window.aggregate` (:issue:`26597`)
11411142
11421143
Reshaping
11431144
^^^^^^^^^

pandas/core/base.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,16 @@ def _try_aggregate_string_function(self, arg, *args, **kwargs):
314314

315315
f = getattr(np, arg, None)
316316
if f is not None:
317-
return f(self, *args, **kwargs)
317+
try:
318+
return f(self, *args, **kwargs)
319+
320+
except (AttributeError, TypeError):
321+
pass
318322

319-
raise ValueError("{arg} is an unknown string function".format(arg=arg))
323+
raise AttributeError(
324+
"'{arg}' is not a valid function for "
325+
"'{cls}' object".format(arg=arg, cls=type(self).__name__)
326+
)
320327

321328
def _aggregate(self, arg, *args, **kwargs):
322329
"""

pandas/tests/window/test_window.py

+16
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,22 @@ def test_numpy_compat(self, method):
439439
with pytest.raises(UnsupportedFunctionCall, match=msg):
440440
getattr(w, method)(dtype=np.float64)
441441

442+
@td.skip_if_no_scipy
443+
@pytest.mark.parametrize("arg", ["median", "var", "std", "kurt", "skew"])
444+
def test_agg_function_support(self, arg):
445+
df = pd.DataFrame({"A": np.arange(5)})
446+
roll = df.rolling(2, win_type="triang")
447+
448+
msg = "'{arg}' is not a valid function for " "'Window' object".format(arg=arg)
449+
with pytest.raises(AttributeError, match=msg):
450+
roll.agg(arg)
451+
452+
with pytest.raises(AttributeError, match=msg):
453+
roll.agg([arg])
454+
455+
with pytest.raises(AttributeError, match=msg):
456+
roll.agg({"A": arg})
457+
442458

443459
class TestRolling(Base):
444460
def setup_method(self, method):

0 commit comments

Comments
 (0)