Medulla Oblongata
Medulla Oblongata

Reputation: 3973

Adding holidays column to pandas dataframe

I have a pandas dataframe object df for the following dates:

>> df.index

DatetimeIndex(['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04',
               '2015-01-05', '2015-01-06', '2015-01-07', '2015-01-08',
               '2015-01-09', '2015-01-10'],
              dtype='datetime64[ns]', name='Date', length=10, freq=None)

I want to add a column to df showing whether or not it is a public holiday:

import pandas as pd
import holidays

df['hols'] = holidays.CountryHoliday('AUS',prov='NSW').get(df.index.to_datetime())

and get the error: AttributeError: 'DatetimeIndex' object has no attribute 'to_datetime'.

If I try

df['hols'] = holidays.CountryHoliday('AUS',prov='NSW').get(pd.to_datetime(df.index))

I get error TypeError: Cannot convert type '<class 'pandas.core.indexes.datetimes.DatetimeIndex'>' to date.

I'm aware of the workalendar package from Adding holidays columns in a Dataframe in Python, but I can't install the package on my university PC.

Upvotes: 5

Views: 3054

Answers (1)

ipj
ipj

Reputation: 3598

Try this solution using lambda function:

df['hols'] = pd.Series(df.index).apply(lambda x: holidays.CountryHoliday('AUS',prov='NSW').get(x)).values

Method get() should receive only one value, not entire index or array. Result when apply to Your data is:

                      hols
Date                      
2015-01-01  New Year's Day
2015-01-02            None
2015-01-03            None
2015-01-04            None
2015-01-05            None
2015-01-06            None
2015-01-07            None
2015-01-08            None
2015-01-09            None
2015-01-10            None

Upvotes: 5

Related Questions