数据的问题,时间不等。import pandas as pd
# Create your dataset
index = pd.date_range('2000/01/01 00:00:00', periods=4, freq='min').to_list()
index[1] = pd.Timestamp('2000/01/01 00:00:10')
values = [0., 100., 100., 200.]
data = pd.Series(values, index)
data
[Out]:
2000-01-01 00:00:00 0.0
2000-01-01 00:00:10 100.0
2000-01-01 00:02:00 100.0
2000-01-01 00:03:00 200.0
当你调用.resample()时,发生这种情况:data.asfreq('T')
[Out]:
2000-01-01 00:00:00 0.0
2000-01-01 00:01:00 NaN
2000-01-01 00:02:00 100.0
2000-01-01 00:03:00 200.0
值被放入相应的分钟中,因为在1和2分钟之间没有数据,值被填充为NaN,
你可以重采样到10秒,向前填充,然后重采样到1分钟。print(data.resample('10s').ffill())
[Out]:
2000-01-01 00:00:00 0.0
2000-01-01 00:00:10 100.0
2000-01-01 00:00:20 100.0
2000-01-01 00:00:30 100.0
2000-01-01 00:00:40 100.0
2000-01-01 00:00:50 100.0
2000-01-01 00:01:00 100.0
2000-01-01 00:01:10 100.0
2000-01-01 00:01:20 100.0
2000-01-01 00:01:30 100.0
2000-01-01 00:01:40 100.0
2000-01-01 00:01:50 100.0
2000-01-01 00:02:00 100.0
2000-01-01 00:02:10 100.0
2000-01-01 00:02:20 100.0
2000-01-01 00:02:30 100.0
2000-01-01 00:02:40 100.0
2000-01-01 00:02:50 100.0
2000-01-01 00:03:00 200.0
print(data.resample('10s').ffill().resample('1t').mean())
[Out]:
2000-01-01 00:00:00 83.333333
2000-01-01 00:01:00 100.000000
2000-01-01 00:02:00 100.000000
2000-01-01 00:03:00 200.000000
在你的问题中,你期望在2分钟到3分钟之间的结果是150,但事实并非如此,因为在这一分钟内的值是100.
也许你会发现.interpolate(method='time')有用,这里有一个到文档的链接。