You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm running into a weird issue with groupby and function closure. For some reason the function closure doesn't work unless I access the grouped series. You can see in agg_before I have a fix flag that will just access the data var.
frompandasimport*importnumpyasnpperiods=1000ind=DatetimeIndex(start='2012/1/1', freq='5min', periods=periods)
df=DataFrame({'high': np.arange(periods), 'low': np.arange(periods)}, index=ind)
defagg_before(hour, func, fix=False):
""" Run an aggregate func on the subset of data. """def_func(data):
d=data.select(lambdax: x.hour<11).dropna()
iffix:
data[data.index[0]]
iflen(d) ==0:
returnNonereturnfunc(d)
return_funcdefafunc(data):
d=data.select(lambdax: x.hour<11).dropna()
returnnp.max(d)
grouped=df.groupby(lambdax: datetime(x.year, x.month, x.day))
closure_bad=grouped.agg({'high': agg_before(11, np.max)})
closure_good=grouped.agg({'high': agg_before(11, np.max, True)})
lambda_good=grouped.agg({'high': afunc})
In [33]: np.__version__
Out[39]: '1.6.2'
In [34]: pandas.__version__
Out[34]: '0.8.0.dev-dc6ce90'
In [35]: closure_bad
Out[35]:
high
2012-01-01 131
2012-01-02 NaN
2012-01-03 NaN
2012-01-04 NaN
In [36]: closure_good
Out[36]:
high
2012-01-01 131
2012-01-02 419
2012-01-03 707
2012-01-04 995
In [37]: lambda_good
Out[37]:
high
2012-01-01 131
2012-01-02 419
2012-01-03 707
2012-01-04 995
Running an agg function that isn't a closure works fine. Any ideas on this?
The text was updated successfully, but these errors were encountered:
I'm running into a weird issue with groupby and function closure. For some reason the function closure doesn't work unless I access the grouped series. You can see in agg_before I have a fix flag that will just access the data var.
Running an agg function that isn't a closure works fine. Any ideas on this?
The text was updated successfully, but these errors were encountered: