scrps93
scrps93

Reputation: 313

Pandas specifying custom holidays

I'm trying to specify business days in a foreign country, but I can't get the pandas function pd.bdate_range() to recognize holidays. My code is as follows:

import pandas as pd
import datetime

weekmask = "Mon Tue Wed Thu Fri"

holidays = [datetime.datetime(2017, 1, 9), datetime.datetime(2017, 3, 20),
            datetime.datetime(2017, 4, 13)]

BdaysCol2017 = pd.bdate_range(start = pd.datetime(2017, 1, 1), 
                              end = pd.datetime(2017, 12, 31), 
                              weekmask = weekmask, 
                              holidays = holidays)

But I get the following error on the holidays parameter:

ValueError: a custom frequency string is required when holidays or weekmask are passed, got frequency B

Why is this? How can I specify custom holidays? Is there a better way to do this?

Thank you

Upvotes: 8

Views: 6722

Answers (2)

Wojciech Moszczyński
Wojciech Moszczyński

Reputation: 3187

I would do that:

import pandas as pd  
from datetime import datetime

weekmask = 'Sun Mon Tue Wed Thu'
exclude = [pd.datetime(2017, 3, 20),
           pd.datetime(2017, 4, 13),
           pd.datetime(2017, 5, 3)]

pd.bdate_range('2017/1/1','2017/12/31',
               freq='C',
               weekmask = weekmask,
               holidays=exclude)

Upvotes: 1

Ezer K
Ezer K

Reputation: 3739

as the docs specify about weekmask and holidays:

only used when custom frequency strings are passed

so you need:

BdaysCol2017 = pd.bdate_range(start = pd.datetime(2017, 1, 1), 
                          end = pd.datetime(2017, 12, 31),
                          freq='C',
                          weekmask = weekmask,
                          holidays=holidays)

Upvotes: 15

Related Questions