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 1 commit
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
11 changes: 7 additions & 4 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,13 @@ 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:
raise ValueError(
"'{arg}' is not a valid function for "
"'{cls}' object".format(arg=arg, cls=type(self).__name__)
)

raise ValueError("{arg} is an unknown string function".format(arg=arg))

Expand Down Expand Up @@ -603,9 +609,6 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis):
keys.append(col)
except (TypeError, DataError):
pass
except ValueError:
# cannot aggregate
continue
except SpecificationError:
raise

Expand Down
22 changes: 0 additions & 22 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,26 +660,6 @@ def validate(self):
else:
raise ValueError("Invalid window {0}".format(window))

def _validate_aggregate(self, arg, *args, **kwargs):
# supported weighted window functions
valid_ops = ["mean", "sum"]

def validate_str(op):
if op not in valid_ops:
raise ValueError(
"{op} function is not supported "
"for weighted windows.".format(op=op)
)

if isinstance(arg, str):
validate_str(arg)
elif isinstance(arg, (list, tuple, dict)):
ops = arg.values() if isinstance(arg, dict) else arg

for op in ops:
if isinstance(op, str):
validate_str(op)

def _prep_window(self, **kwargs):
"""
Provide validation for our window type, return the window
Expand Down Expand Up @@ -836,8 +816,6 @@ def f(arg, *args, **kwargs):
)
@Appender(_shared_docs["aggregate"])
def aggregate(self, arg, *args, **kwargs):
self._validate_aggregate(arg)

result, how = self._aggregate(arg, *args, **kwargs)
if result is None:

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def test_agg_function_support(self, arg):
ser = pd.DataFrame({"A": np.arange(5), "B": np.arange(5)})
roll = ser.rolling(2, win_type="triang")

msg = "std function is not supported for weighted windows."
msg = "'std' is not a valid function for 'Window' object"
with pytest.raises(ValueError, match=msg):
roll.agg(arg)

Expand Down