0

I fail to find a way to properly name custom aggregate functions applied to rolling windows. This answer explains it well for groupby aggregates. I've tried using pd.NamedAggregates, like so

df
.rolling(f"{num_days_window + 1}D", min_periods=day_length)            
.aggregate(time_mean=pd.NamedAgg(column="time", aggfunc=lambda w: window_daily_stats(w, np.mean)),
           time_std=pd.NamedAgg(column="time", aggfunc=lambda w: window_daily_stats(w, np.std)))

Nested dictionaries for naming are deprecated, so that's not an option. Passing in tuples also doesn't work.

.rolling(f"{num_days_window + 1}D", min_periods=day_length)
.aggregate(time_mean=("time", lambda w: window_daily_stats(w, np.mean)),
           time_std=("time", lambda w: window_daily_stats(w, np.std)))

In both cases the error is the same:

TypeError: aggregate() missing 1 required positional argument: 'func'

The way I currently do it is I pass the aggregate function a dict containing column: list of functions pairs, but in that case the resulting columns are named

('time', '<lambda>'),
('time', '<lambda>'), 

Which unfortunately doesn't give me uniquely valued Index objects for columns.

All in all my question is, how do I create named aggregates for custom functions for rolling windows?

Grinjero
  • 329
  • 1
  • 6
  • Does this work? `df[['time_mean', 'time_std']] = df.time.rolling(...).agg(['mean', 'std']])` named aggregation does not work for rolling agg. – Emma Oct 14 '21 at 15:55
  • Unfortunately, I need to apply a specific custom function to the rolling window – Grinjero Oct 15 '21 at 07:25
  • you can do `df[['time_mean', 'time_std']] = df.time.rolling(...).agg([lambda w: window_daily_stats(w, np.mean), lambda w: window_daily_stats(w, np.std)])`. You can pass functions or functions name (string) in list. – Emma Oct 15 '21 at 14:20

0 Answers0