Skip to content

ENH: Raise ValueError for unsupported Window functions #27275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/window/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down