Python: resample on a rolling basis

dxw914 注册会员
6 days ago

You apply last as if rolling gave you dataframe; it doesn't (because it's actually "incomplete" as you can see below).

A general tip is that you can grab whatever you get from the previous step, and use help on it. In this case

x = df1.resample('1D').last().rolling(7)

which gives you a very extensive manual.

What's missing from your problem is that you haven't actually precisely specified what you want to roll. Do you want a rolling mean? That gives you a hint that you want to use .mean() on the rolled data.

More specifically, in this case, it's probably most helpful to use the timedelta, which will make the code even more clear, and help you with corner-cases as opposed to using the integer count

rolled_df = df1.rolling(datetime.timedelta(days=7)).mean()

About the Author

Question Info

Publish Time
6 days ago
Update Time
6 days ago