简体   繁体   English

如何让熊猫知道假期?

[英]How to make Pandas aware of holiday dates?

I've been looking at the documentation here 我一直在看这里的文档

I have a dataframe which contains a daily time series (Note that 2013-03-29 is a holiday and mydf contains 2013-03-28). 我有一个包含每日时间序列的数据mydf (请注意,2013-03-29是假日, mydf包含2013-03-28)。

import pandas as pd

business_month_end_dates = pd.date_range('2010-01-31', '2014-04-30', freq='BM')

business_month_end_dates

DatetimeIndex(['2010-02-26', '2010-03-31', '2010-04-30', '2010-05-31',
           '2010-06-30', '2010-07-30', '2010-08-31', '2010-09-30',
           '2010-10-29', '2010-11-30', '2010-12-31', '2011-01-31',
           '2011-02-28', '2011-03-31', '2011-04-29', '2011-05-31',
           '2011-06-30', '2011-07-29', '2011-08-31', '2011-09-30',
           '2011-10-31', '2011-11-30', '2011-12-30', '2012-01-31',
           '2012-02-29', '2012-03-30', '2012-04-30', '2012-05-31',
           '2012-06-29', '2012-07-31', '2012-08-31', '2012-09-28',
           '2012-10-31', '2012-11-30', '2012-12-31', '2013-01-31',
           '2013-02-28', '2013-03-29', '2013-04-30', '2013-05-31',
           '2013-06-28', '2013-07-31', '2013-08-30', '2013-09-30',
           '2013-10-31', '2013-11-29', '2013-12-31', '2014-01-31',
           '2014-02-28', '2014-03-31', '2014-04-30'],
          dtype='datetime64[ns]', freq='BM')

I then create Business Year End Dates: 然后我创建营业年度结束日期:

business_year_end_dates =  pd.date_range(business_month_end_dates[0],
                                 business_month_end_dates[len(business_month_end_dates)-1],
                                 freq=BYearEnd(month=3))

business_year_end_dates

DatetimeIndex(['2010-03-31', '2011-03-31', '2012-03-30', '2013-03-29',
           '2014-03-31'],
          dtype='datetime64[ns]', freq='BA-MAR')

My expected output is : 我的预期输出是:

DatetimeIndex(['2010-03-31', '2011-03-31', '2012-03-30', '2013-03-28',
       '2014-03-31'],
      dtype='datetime64[ns]', freq='BA-MAR')

Should Pandas understand via BM business month end frequency that the 2013-03-29 is a holiday? 熊猫应该通过BM business month end frequency了解2013-03-29是假期吗?

If not how do I make Pandas aware such that when I use BM it uses the previous working day in this case 2013-03-28? 如果不是,我如何让Pandas知道,当我使用BM时,它会使用前一个工作日2013-03-28?

Try something like this: 尝试这样的事情:

import numpy as np
import pandas as pd
from pandas.tseries.offsets import CustomBusinessMonthEnd

index = pd.date_range('2010-01-31', '2014-04-30', freq='BM')
df = pd.DataFrame(data=np.random.rand(len(index), 2), columns=['a', 'b'], index=index)

holidays = ['2013-03-29']

df = df.resample(rule=CustomBusinessMonthEnd(holidays=holidays)).last()

df = df.loc[df.index.month==3]

Output: 输出:

>>>df.index
>>>DatetimeIndex(['2010-03-31', '2011-03-31', '2012-03-30', '2013-03-28', '2014-03-31'], dtype='datetime64[ns]', freq='12CBM')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM