Pandas/Python:查找时间序列中缺少的值,为缺少的值插入新的时间戳和nan值
我创建以下数据帧:Pandas/Python:查找时间序列中缺少的值,为缺少的值插入新的时间戳和nan值,python,pandas,datetime,time,series,Python,Pandas,Datetime,Time,Series,我创建以下数据帧: import pandas as pd d = {'T': [1, 2, 4, 15], 'H': [3, 4, 6, 8]} df = pd.DataFrame(data=d, index=['10.09.2018 13:15:00','10.09.2018 13:30:00', '10.09.2018 14:00:00', '10.09.2018 22:00:00']) df.index = pd.to_datetime(df.index) 并得到以下结果 O
import pandas as pd
d = {'T': [1, 2, 4, 15], 'H': [3, 4, 6, 8]}
df = pd.DataFrame(data=d, index=['10.09.2018 13:15:00','10.09.2018 13:30:00', '10.09.2018 14:00:00', '10.09.2018 22:00:00'])
df.index = pd.to_datetime(df.index)
并得到以下结果
Out[30]:
T H
2018-10-09 13:15:00 1 3
2018-10-09 13:30:00 2 4
2018-10-09 14:00:00 4 6
2018-10-09 22:00:00 15 8
如您所见,13:45:00缺少一个值,而14:00和22:00之间缺少很多值
是否有一种方法可以自动查找缺失的值,插入一行,其中包含缺失的时间戳和缺失时间的nan值
我想做到这一点:
Out[30]:
T H
2018-10-09 13:15:00 1 3
2018-10-09 13:30:00 2 4
2018-10-09 13:45:00 nan nan
2018-10-09 14:00:00 4 6
2018-10-09 14:15:00 nan nan
...
2018-10-09 21:45:00 nan nan
2018-10-09 22:00:00 15 8
您可以使用正确的时间步作为索引创建第二个数据帧,并将其与原始数据连接起来。以下代码在我的案例中起作用
# your code
import pandas as pd
d = {'T': [1, 2, 4, 15], 'H': [3, 4, 6, 8]}
df = pd.DataFrame(data=d, index=['10.09.2018 13:15:00','10.09.2018 13:30:00', '10.09.2018 14:00:00', '10.09.2018 22:00:00'])
df.index = pd.to_datetime(df.index)
# generate second dataframe with needed index
timerange = pd.date_range('10.09.2018 13:15:00', periods=40, freq='15min')
df2 = pd.DataFrame(index=timerange)
# join the original dataframe with the new one
newdf = df.join(df2, how='outer')
df.resample(“900S”).sum()
?使用df.asfreq('15T')