如何将熊猫的营业月份与日历日延迟

  • 本文关键字:日历 延迟 熊猫 pandas
  • 更新时间 :
  • 英文 :


我想将时间序列数据滞后几个月。例如,如果日历日为2018年7月12日,则1个月的滞后时间应为2018年10月26日、2、3个月等。

import pandas as pd
df = pd.date_range('2018-12-07','2018-12-10',freq = 'D')
df.shift(-1 , freq = 'BM')

预期结果

2018-10-26
2018-10-25
2018-10-24

pandas时间序列模块有一个名为BDay的有用的工作日功能,可以帮助实现这一点。

from pandas.tseries.offsets import BDay
example_day = pd.datetime(2018, 12, 7) # 12/7/2018
shifted_day = example_day - BDay(31) # Shifted by 31 business days
shifted_day # This is now a pandas TimeStamp object
# Timestamp('2018-10-25 00:00:00')
# You can also apply this shift to a Pandas Date Range
df = pd.date_range('2018-12-07','2018-12-10',freq = 'D')
result = df - BDay(31) # Keep in mind that 12/8 -> 12/10 will share the same shift day due to the weekend between them

最新更新