I want to resample data column using forward fill ffill
and backward fill bfill
at the frequency of 1min
while grouping df
by id
column.
df
:
id timestamp data
1 1 2017-01-02 13:14:53.040 10.0
2 1 2017-01-02 16:04:43.240 11.0
...
4 2 2017-01-02 15:22:06.540 1.0
5 2 2017-01-03 13:55:34.240 2.0
...
I used:
pd.DataFrame(df.set_index('timestamp').groupby('id', sort=True)['data'].resample('1min').ffill().bfill())
How can I add an additional condition, by resampling within the window of past 10 days from now? So the last timestamp
reading is now and the first timestamp
reading is datetime.datetime.now() - pd.to_timedelta("10day"). The goal is to have the same number of readings for each id
group.
Update:
Tried:
start = datetime.datetime.now() - pd.to_timedelta("10day")
end = datetime.datetime.now()
r = pd.to_datetime(pd.date_range(start=start, end=end, freq='1h'))
pd.DataFrame(df.reset_index().set_index('timestamp').groupby('id', sort=True).reindex(r)['data'].resample('1h').ffill().bfill())
and returned:
AttributeError: 'DataFrameGroupBy' object has no attribute 'reindex'
so I'm not supposed to apply reindex
for groupby
object, is there a way that I can work around it?