簡體   English   中英

熊貓根據給定(任意)的datetimeindex重新采樣(例如使用最近的值)

[英]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_datesdf的列。

也許有些晚,但是這是一個使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM