如何在大熊猫中进行日历滞后

先生

我正在尝试对要分析的时间序列数据进行日历延迟。•对于x个月的延迟,请返回x个月前的值。(从5月17日起3个月的滞后时间将是2月17日的数据)。

有没有直接在熊猫中可用的解决方案?

df_list = pd.date_range(start=min_date, end=max_date,freq='D').to_frame(index=False)
df_list.columns = ['name']
df_list.set_index('name',inplace = True)
df = df_list.reindex(df_list.index - pd.to_timedelta(30, unit='d'), 
method='nearest')

这段代码无法正确返回1月3日的1月3日,最好是1月2日。

3月31日,30日,29日,28日应为2月28日(均使用1个月的滞后时间)。

洛兹

这是pd.offsets.DateOffset(months=1)它甚至正确地获得了leap年。它利用dateutil.relavitedelta逻辑,可以根据需要进行日期减法。

import pandas as pd
dates = pd.to_datetime(['2018-03-01', '2018-03-31', '2018-03-30', 
                        '2018-03-29', '2018-03-28', '2016-03-31'])
df = pd.DataFrame({'dates': dates})
#       dates
#0 2018-03-01
#1 2018-03-31
#2 2018-03-30
#3 2018-03-29
#4 2018-03-28
#5 2016-03-31

df.dates - pd.offsets.DateOffset(months=1)
#0   2018-02-01
#1   2018-02-28
#2   2018-02-28
#3   2018-02-28
#4   2018-02-28
#5   2016-02-29
#Name: dates, dtype: datetime64[ns]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章