[英]Pandas resample on given (arbitrary) datetimeindex (using e.g. nearest)
Is it possible to use pandas.resample
with a given (arbitrary) DatetimeIndex (using eg nearest
option with a given time window), instead of a the rule
string for regular dates? 是否可以将
pandas.resample
与给定的(任意的)DatetimeIndex(例如,使用具有给定时间窗口的nearest
选项)一起使用,而不是将rule
字符串用于常规日期?
EDIT: 编辑:
Example: 例:
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')
And new_df
should now have as DatetimeIndex custom_dates
and the columns from df
. 现在
new_df
应该具有datetimeIndex custom_dates
和df
的列。
Maybe a bit late, but here is a solution that uses reindex
which supports your desired nearest
option: 也许有些晚,但是这是一个使用
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'))
Output: 输出:
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.