Reputation: 13582
I am doing some comparisons, in order to find out if a specific date is in a specific variable, using the isin
.
In my working test, the variable that I am using to compare holidays
, is a DatetimeIndex
with size (267,)
, as one can see bellow:
And looks like the following:
I am now exploring a different approach, and the variable that I have now is a DataFrame
with size (261, 0)
.
And looks like the following:
How should I go in order to convert the Dataframe (cal
) to a DatetimeIndex?
I will leave, bellow, the code that I am using for this new approach, as it may be helpful:
import pandas as pd
from pandas.tseries.holiday import *
from datetime import date
from fbprophet import *
from fbprophet.make_holidays import make_holidays_df
year_list = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
PTBusinessCalendar = make_holidays_df(year_list=year_list, country='PT')
ESBusinessCalendar = make_holidays_df(year_list=year_list, country='ES')
iberian = [PTBusinessCalendar, ESBusinessCalendar]
IberianBusinessCalendar = pd.concat(iberian).sort_values('ds').reset_index(drop=True)
dr = pd.date_range(start='2008-01-01', end='2020-02-1')
df = pd.DataFrame()
df['Date'] = dr
cal = IberianBusinessCalendar.drop('holiday', 1)
cal['Holiday'] = IberianBusinessCalendar['ds'].dt.date
cal['Holiday'] = pd.to_datetime(cal['Holiday'])
cal = cal.drop('ds', 1).set_index(cal['Holiday']).drop('Holiday', 1)
df['Holiday'] = df['Date'].isin(cal).astype(int)
print(df)
Whose output is the following (and as one can see, it is not picking up the Holiday in the Date 2008-01-01
:
Upvotes: 2
Views: 168
Reputation: 13582
As my goal was doing the comparison, changing the Variable Type was not required. A simple change in the final query works fine:
df['Holiday'] = df['Date'].isin(cal['Holiday']).astype(int)
print(df)
Now the output is what I was looking for (as one can see, it picks up that in 01-01 there is an holiday:
Upvotes: 1
Reputation: 1541
cal['Holiday'] = pd.to_datetime(cal['Holiday'])
cal = cal.set_index('Holiday')
Upvotes: 0