-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: rolling.apply will pass Series to function #20584
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,7 +314,7 @@ def _center_window(self, result, window): | |
def aggregate(self, arg, *args, **kwargs): | ||
result, how = self._aggregate(arg, *args, **kwargs) | ||
if result is None: | ||
return self.apply(arg, args=args, kwargs=kwargs) | ||
return self.apply(arg, raw=False, args=args, kwargs=kwargs) | ||
return result | ||
|
||
agg = aggregate | ||
|
@@ -954,23 +954,53 @@ def count(self): | |
Parameters | ||
---------- | ||
func : function | ||
Must produce a single value from an ndarray input | ||
\*args and \*\*kwargs are passed to the function""") | ||
Must produce a single value from an ndarray input if ``raw=True`` | ||
or a Series if ``raw=False`` | ||
raw : bool, default None | ||
* ``False`` : passes each row or column as a Series to the | ||
function. | ||
* ``True`` or ``None`` : the passed function will receive ndarray | ||
objects instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you indicate something about the deprecation / changing default? |
||
If you are just applying a NumPy reduction function this will | ||
achieve much better performance. | ||
|
||
The `raw` parameter is required and will show a FutureWarning if | ||
not passed. In the future `raw` will default to False. | ||
|
||
.. versionadded:: 0.23.0 | ||
|
||
\*args and \*\*kwargs are passed to the function""") | ||
|
||
def apply(self, func, raw=None, args=(), kwargs={}): | ||
from pandas import Series | ||
|
||
def apply(self, func, args=(), kwargs={}): | ||
# TODO: _level is unused? | ||
_level = kwargs.pop('_level', None) # noqa | ||
window = self._get_window() | ||
offset = _offset(window, self.center) | ||
index, indexi = self._get_index() | ||
|
||
# TODO: default is for backward compat | ||
# change to False in the future | ||
if raw is None: | ||
warnings.warn( | ||
"Currently, 'apply' passes the values as ndarrays to the " | ||
"applied function. In the future, this will change to passing " | ||
"it as Series objects. You need to specify 'raw=True' to keep " | ||
"the current behaviour, and you can pass 'raw=False' to " | ||
"silence this warning", FutureWarning, stacklevel=3) | ||
raw = True | ||
|
||
def f(arg, window, min_periods, closed): | ||
minp = _use_window(min_periods, window) | ||
return _window.roll_generic(arg, window, minp, indexi, closed, | ||
offset, func, args, kwargs) | ||
if not raw: | ||
arg = Series(arg, index=self.obj.index) | ||
return _window.roll_generic( | ||
arg, window, minp, indexi, | ||
closed, offset, func, raw, args, kwargs) | ||
|
||
return self._apply(f, func, args=args, kwargs=kwargs, | ||
center=False) | ||
center=False, raw=raw) | ||
|
||
def sum(self, *args, **kwargs): | ||
nv.validate_window_func('sum', args, kwargs) | ||
|
@@ -1498,8 +1528,9 @@ def count(self): | |
@Substitution(name='rolling') | ||
@Appender(_doc_template) | ||
@Appender(_shared_docs['apply']) | ||
def apply(self, func, args=(), kwargs={}): | ||
return super(Rolling, self).apply(func, args=args, kwargs=kwargs) | ||
def apply(self, func, raw=None, args=(), kwargs={}): | ||
return super(Rolling, self).apply( | ||
func, raw=raw, args=args, kwargs=kwargs) | ||
|
||
@Substitution(name='rolling') | ||
@Appender(_shared_docs['sum']) | ||
|
@@ -1756,8 +1787,9 @@ def count(self, **kwargs): | |
@Substitution(name='expanding') | ||
@Appender(_doc_template) | ||
@Appender(_shared_docs['apply']) | ||
def apply(self, func, args=(), kwargs={}): | ||
return super(Expanding, self).apply(func, args=args, kwargs=kwargs) | ||
def apply(self, func, raw=None, args=(), kwargs={}): | ||
return super(Expanding, self).apply( | ||
func, raw=raw, args=args, kwargs=kwargs) | ||
|
||
@Substitution(name='expanding') | ||
@Appender(_shared_docs['sum']) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this have any consequences for the user?
(but, on the other hand, I don't think it is worth to add a
raw
keyword just to be able to deprecate the behaviour and change it more slowly)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, this is a very special case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not that a special case, it happens when the user passes a custom function to
r.agg(..)
, which eg is done in the documentation about it: http://pandas.pydata.org/pandas-docs/stable/computation.html#aggregationSo just as with
apply
, if the user has a function that behaves differently for a Series than an array, this will break.