-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Positional Arguments Passed as a Keyword Argument in Custom Rolling Aggregation #34605
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
Comments
Could you give an example of the buggy behavior? The API doesn't have a traditional
|
Sorry, I was mistaken in the previous investigation. The issue is more intricate than it seems at a glance. def foo(*args, **kwargs):
assert len(args) == 2, 'Too a few pos argumnets.'
return float('nan')
df = pd.DataFrame(data={'X': range(5)},
index=[0, 0, 1, 1, 1])
df.rolling(1) \
.apply(foo, raw=True, args=(42,)) # ok
df.groupby(level=0) \
.rolling(1) \
.apply(foo, raw=True, args=(42,)) # failed The second So, this snippet elucidates where buggy behavour is from. Method class WindowGroupByMixin(GroupByMixin):
def _apply(
self,
func: Callable,
....
name: Optional[str] = None,
....
):
def f(x, name=name, *args):
x = self._shallow_copy(x)
if isinstance(name, str):
return getattr(x, name)(*args, **kwargs)
return x.apply(name, *args, **kwargs) |
It appears this is fixed on master now, and will be fixed in the pandas 1.1 release. I think #34052 inadvertently fixed this. It'd be good to add a test to confirm.
|
take |
Hi, I'm preparing a tutorial on pytest for PyData Amsterdam 2020. The tutorial — which is part of a longer serie — should prepare them to start contributing on pandas. I thought this would be a good issue to illustrate how people can get started with pytest. I plan to work these days until the 17/06 (day of the tutorial). I should be able to create a pull request for this, so I'm not taking it for the sake of the tutorial. |
@gglanzani This is great! Thanks for helping and good luck with your talk. |
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Problem description
According to docstring of
Rolling.apply
argumentsargs
andkwargs
allow to pass to a custom function positional and keyword arguments enumerated inkwargs
. However,Rolling.apply
passes both of them as keyword arguments. In other word, it passes keywordargs
consisting of "positional" arguments. This is definitely buggy behaviour.The reason is that a closure function is built in a wrong way. The issue states for
cython
backend as well asnumba
backend. The possible should looks like the following.The text was updated successfully, but these errors were encountered: