2

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:

enter image description here

And looks like the following:

enter image description here

I am now exploring a different approach, and the variable that I have now is a DataFrame with size (261, 0).

enter image description here

And looks like the following:

enter image description here

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:

enter image description here

1
  • 1
    You can read more on converting the data frame index to a datetime index in this Medium article. Commented Jul 4, 2020 at 21:02

2 Answers 2

1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0
cal['Holiday'] = pd.to_datetime(cal['Holiday'])
cal = cal.set_index('Holiday')

1 Comment

I have added the code that I am using and as you will see, I have tried a similar approach to what you suggested, however, it didn't change the Type of the variable. Therefore, it is also not picking up in the comparison (as one can see in the output).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.