Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

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 @@ -1130,6 +1130,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
19 changes: 17 additions & 2 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 @@ -554,7 +560,16 @@ def is_any_frame():
return result, True
elif is_list_like(arg):
# we require a list, but not an 'str'
return self._aggregate_multiple_funcs(arg, _level=_level, _axis=_axis), None
try:
return self._aggregate_multiple_funcs(arg, _level=_level, _axis=_axis), None

# if no results
except ValueError:
raise ValueError(
"'{arg}' is not a valid set of functions for "
"'{cls}' object".format(arg=", ".join(arg), cls=type(self).__name__)
)

else:
result = None

Expand Down
1 change: 1 addition & 0 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ class Window(_Window):
Set the labels at the center of the window.
win_type : str, default None
Provide a window type. If ``None``, all points are evenly weighted.
Other types are only applicable for `mean` and `sum` functions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be on .agg so I wouldn't add anything here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agg is using shared_docs. I was avoiding to copy it and make a single line change. Will remove it only if no objection

See the notes below for further information.
on : str, optional
For a DataFrame, column on which to calculate
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,22 @@ def test_numpy_compat(self, method):
with pytest.raises(UnsupportedFunctionCall, match=msg):
getattr(w, method)(dtype=np.float64)

@pytest.mark.parametrize(
"arg", ["std", ["mean", "std"], ("mean", "std"), {"A": "mean", "B": "std"}]
)
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")

if isinstance(arg, (list, tuple)):
msg = ("'{arg}' is not a valid set of functions for "
"'Window' object".format(arg=", ".join(arg)))
else:
msg = "'std' is not a valid function for 'Window' object"

with pytest.raises(ValueError, match=msg):
roll.agg(arg)


class TestRolling(Base):
def setup_method(self, method):
Expand Down