使用前几天同一小时的平均值填充 NaN



我想使用前几天相同小时-分钟的平均值来填充 NaN。为了简化起见,这是我的 df 的一个例子。

timstamp         data
22/04/2016 09:00 1
22/04/2016 09:05 2
...
23/04/2016 09:00 3
23/04/2016 09:05 4
...
24/04/2016 09:00 5
24/04/2016 09:05 6
...
25/04/2016 09:00 7
25/04/2016 09:05 8
...
25/04/2016 10:00 NaN
25/04/2016 10:05 NaN

真实数据包含许多天,间隔连续 5 分钟。

df = df.groupby(df.index.minute).fillna(df.data.rolling(3).mean())试图从过去几天的前一小时分钟开始做滚动平均值,但没有奏效。

另一种df = df.groupby(df.index.minute).ffill()方法从前两行(即 7 和 8)中获取值,这些值来自同一天前一小时的相同分钟数。

但是,我想要以下结果:

timstamp         data
22/04/2016 09:00 1
22/04/2016 09:05 2
...
23/04/2016 09:00 3
23/04/2016 09:05 4
...
24/04/2016 09:00 5
24/04/2016 09:05 6
...
25/04/2016 09:00 7
25/04/2016 09:05 8
25/04/2016 10:00 3
25/04/2016 10:05 4

其中值 3(倒数第二行)是前几天同一小时-分钟的值的平均值(平均值为 1、3 和 5),4(最后一行)是 2、4 和 6 的平均值。考虑到我的 df 的大小,我想从之前的几十天中取平均值。

编辑
我越来越近了。使用以下代码,数据的平均值按我想要的类似小时和分钟计算:

df.set_index('timstamp', inplace=True)
df=df.groupby([df.index.hour, df.index.minute]).mean()
df.index.names = ["hour", "minute"]

但是,它使用整个数据来获取小时-分钟平均值。我想要的是仅使用前几天的相同小时分钟数,我可以在计算中设置过去天数。然后,应使用所得平均值来填充NaN。

让我们试试这个:

# time sample every 5 mins
idx = pd.date_range('2018-01-01', '2018-01-31', freq='300s')
np.random.seed(2019)
# create toy data
df = pd.DataFrame({'idx':idx,
'data':np.random.uniform(0,5, len(idx))})
df.loc[np.random.uniform(0,1,len(idx)) > 0.95, 'data'] = None
# means by the hour, can also use median
means = df.resample('H', on='idx').data.mean()
# get the timestamp on the hour
df['hour'] = df['idx'] - pd.to_timedelta(df.idx.dt.minute, unit='m')
# get the hour stamp of previous day
df['hour'] -= pd.to_timedelta(1, unit='d')
# update NaN
# df.loc[df.data.isna(), 'data'] = means[nan_hour]
# the original mapping raised a ValueError due to duplicates in nan_hour
df.loc[df.data.isna(), 'data'] = df.loc[df.data.isna(), 'hour'].   
replace({'hour': means})

最新更新