[英]Pandas resample on given (arbitrary) datetimeindex (using e.g. nearest)
是否可以将pandas.resample
与给定的(任意的)DatetimeIndex(例如,使用具有给定时间窗口的nearest
选项)一起使用,而不是将rule
字符串用于常规日期?
编辑:
例:
dates = pd.DatetimeIndex(['2000-01-01 12:00:00', '2000-01-03 13:00:00', '2000-01-05 15:00:00', '2000-01-09 10:00:00'])
df = pd.DataFrame({'dummy': dates}, index=dates)
custom_dates = pd.DatetimeIndex(['2000-01-02 09:00:00', '2000-01-05 22:00:00', '2000-01-10 15:00:00'])
new_df = df.resample(custom_dates, method='nearest')
现在new_df
应该具有datetimeIndex custom_dates
和df
的列。
也许有些晚,但是这是一个使用reindex
的解决方案,它支持您所需的nearest
选项:
import pandas as pd
dates = pd.DatetimeIndex(['2000-01-01 12:00:00', '2000-01-03 13:00:00', '2000-01-05 15:00:00', '2000-01-09 10:00:00'])
df = pd.DataFrame({'dummy': dates}, index=dates)
custom_dates = pd.DatetimeIndex(['2000-01-02 09:00:00', '2000-01-05 22:00:00', '2000-01-10 15:00:00'])
df.reindex(custom_dates, method='nearest', tolerance=pd.Timedelta(2, 'D'))
输出:
dummy
2000-01-02 09:00:00 2000-01-01 12:00:00
2000-01-05 22:00:00 2000-01-05 15:00:00
2000-01-10 15:00:00 2000-01-09 10:00:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.