Skip to content

BUG: Unable to pass additional arguments to resample().apply() #14635

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 13 additions & 3 deletions pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,19 @@ def aggregate(self, arg, *args, **kwargs):
self._set_binner()
result, how = self._aggregate(arg, *args, **kwargs)
if result is None:
return self._groupby_and_aggregate(arg,
*args,
**kwargs)
# It seems there is an ambiguity in having both named args
# and variadic in the same function, so we need to distinguish here,
# otherwise the first element of args is "eaten" by grouper in
# _groupby_and_aggregate.
if len(args) == 0:
return self._groupby_and_aggregate(arg,
Copy link
Contributor

Choose a reason for hiding this comment

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

no you need to figure out the reason and not put in hacks
if u can't then can simply construct the args (you make a tuple) and pass to the function
but would like to see where it's not getting passed thru

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, I disappeared for a long time!
Yes, I know it was a hack and I agree it's not a real solution... but I don't know the internals of the code, so I'm not sure I can figure this out...

The problem in my opinion is the definition of the function itself: it seems like you cannot have a function with a named argument with a default value, immediately followed by a variadic part, because the interpreter has no idea where to put the first argument (in the named one or as the first parameter of the variadic?).

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that *args can be removed from _groupby_and_aggregate. this should solve the issue I think. IOW, you can only pass kwargs except for named args. The grouper arg is really internally anyhow.

*args,
**kwargs)
else:
return self._groupby_and_aggregate(arg,
None,
*args,
**kwargs)

return result

Expand Down
11 changes: 11 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,17 @@ def test_resample_datetime_values(self):
res = df['timestamp'].resample('2D').first()
tm.assert_series_equal(res, exp)

def test_resample_additional_args(self):
# Issue 14615

rng = date_range('1/1/2011', periods = 72, freq = 'H')
rng_exp = date_range('1/1/2011', periods = 3, freq = 'D')
vals = np.tile(np.arange(0, 24), 3)
ts = Series(vals, index = rng)
ts_exp = Series([286, 286, 286], index = rng_exp)
res = ts.resample("D").apply(lambda x, y: np.sum(x) + y, 10)
tm.assert_series_equal(res, ts_exp)


class TestPeriodIndex(Base, tm.TestCase):
_multiprocess_can_split_ = True
Expand Down