簡體   English   中英

如何讓熊貓知道假期?

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

我一直在看這里的文檔

我有一個包含每日時間序列的數據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')

然后我創建營業年度結束日期:

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')

我的預期輸出是:

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

熊貓應該通過BM business month end frequency了解2013-03-29是假期嗎?

如果不是,我如何讓Pandas知道,當我使用BM時,它會使用前一個工作日2013-03-28?

嘗試這樣的事情:

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]

輸出:

>>>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