如何用熊猫填充丢失的数据时间行

时间:2019-05-07 05:11:17

标签: pandas dataframe datetime

index                         valuve
2017-01-25 01:00:00:00          1
2017-01-25 02:00:00:00          5
2017-01-25 03:00:00:00          7
2017-01-25 07:00:00:00          34
2017-01-25 20:00:00:00          45
2017-01-25 24:00:00:00          45
2017-01-26 1:00:00:00           31

此数据帧是每天24小时记录,但缺少一些记录。如何将丢失的行插入正确的位置,并将'nan'填入相应的值?

1 个答案:

答案 0 :(得分:1)

这里的日期时间24H很复杂,因此有必要replace23H并添加一个小时。上次使用DataFrame.asfreq为24H DatetimeIndex添加缺失值:

mask = df.index.str.contains(' 24:')
idx = df.index.where(~mask, df.index.str.replace(' 24:', ' 23:'))
idx = pd.to_datetime(idx, format='%Y-%m-%d %H:%M:%S:%f')
df.index = idx.where(~mask, idx + pd.Timedelta(1, unit='H'))
df = df.asfreq('H')
print (df)
                     valuve
index                      
2017-01-25 01:00:00     1.0
2017-01-25 02:00:00     5.0
2017-01-25 03:00:00     7.0
2017-01-25 04:00:00     NaN
2017-01-25 05:00:00     NaN
2017-01-25 06:00:00     NaN
2017-01-25 07:00:00    34.0
2017-01-25 08:00:00     NaN
2017-01-25 09:00:00     NaN
2017-01-25 10:00:00     NaN
2017-01-25 11:00:00     NaN
2017-01-25 12:00:00     NaN
2017-01-25 13:00:00     NaN
2017-01-25 14:00:00     NaN
2017-01-25 15:00:00     NaN
2017-01-25 16:00:00     NaN
2017-01-25 17:00:00     NaN
2017-01-25 18:00:00     NaN
2017-01-25 19:00:00     NaN
2017-01-25 20:00:00    45.0
2017-01-25 21:00:00     NaN
2017-01-25 22:00:00     NaN
2017-01-25 23:00:00     NaN
2017-01-26 00:00:00    45.0
2017-01-26 01:00:00    31.0