简体   繁体   English

熊猫根据给定(任意)的datetimeindex重新采样(例如使用最近的值)

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

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.

相关问题 Pandas 按非时间序列列(例如价格)对数据重新采样 - Pandas resample data by a non-timeseries column (e.g. Price) Python-pandas-Datetimeindex:分析步数滚动的最蟒蛇策略是什么? (例如每天某些时间) - Python-pandas - Datetimeindex: What is the mosty pythonic strategy to analyse rolling with steps? (e.g. certain hours for each day) Pandas resample函数不能在DateTimeIndex上工作 - Pandas resample function not working on DateTimeIndex 如何使用NumPy / SciPy计算运动/运行/滚动任意函数(例如峰度和偏度) - How to calculate moving / running / rolling arbitrary function (e.g. kurtosis & skewness) using NumPy / SciPy Pandas:在给定时间(例如每一天)对插值时间序列数据进行采样的更简单方法 - Pandas: easier way to sample interpolated time series data at given times (e.g. every full day) 基于 DatetimeIndex 变量重新采样 Pandas Dataframe - Resample Pandas Dataframe based on a DatetimeIndex variable 尝试使用 DatetimeIndex 重新采样 Pandas dataframe 时出现 AttributeError - AttributeError while trying to resample a Pandas dataframe with DatetimeIndex Pandas:重新采样 dataframe 以匹配不同 dataframe 的 DatetimeIndex - Pandas: resample a dataframe to match a DatetimeIndex of a different dataframe pandas Sqlite 和重采样错误仅对 DatetimeIndex 有效 - pandas Sqlite and resample error Only valid with DatetimeIndex Pandas DateTimeIndex 多个 groupby 或 resample 聚合 - Pandas DateTimeIndex multiple groupby or resample aggregation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM