Validate date as holiday or not

Asked

Viewed 154 times

2

Hello, good afternoon, sir! I am creating a Dataframe in which I need to validate whether a day is a holiday or not, so I created a time series in hand and made two loops to validate whether the date that was imported from csv and the date I entered in hand are equal, below follows the code.

feriados = ['2015-01-01','2015-02-17','2015-04-03','2015-04-05','2015-04-21','2015-05-01','2015-06-04','2015-09-07',
        '2015-10-12','2015-11-02','2015-11-15','2015-12-25','2016-01-01','2016-02-09','2016-03-25','2016-03-27',
        '2016-04-21','2016-05-01','2016-05-26','2016-06-04','2016-09-07','2016-10-12','2016-11-02','2016-11-15',
        '2016-12-25','2017-01-01','2017-02-28','2017-04-14','2017-04-16','2017-04-21','2017-05-01','2017-05-26',
        '2017-06-15','2017-09-07','2017-10-12','2017-11-02','2017-11-15','2017-12-25','2018-01-01','2018-02-13',
        '2018-03-30','2018-04-01','2018-04-21','2018-05-01','2018-05-31','2018-09-07','2018-10-12','2018-11-02',
        '2018-11-15','2018-12-25','2019-01-01','2019-03-05','2019-04-19','2019-04-21','2019-05-01','2019-06-20',
        '2019-09-07','2019-10-12','2019-11-02','2019-11-15','2019-12-25']
feriados = pd.to_datetime(feriados, format='%Y-%m-%d')
feriados = pd.DataFrame({'data':feriados})

for dataD in dados['data']:
        for dataF in feriados['data']:
            if dataD == dataF:
                dados['feriado'] = 1
            else:
                dados['feriado'] = 0

Ai, when I go to check if he set some date as holiday, getting the value 1 he does not return me anything. :/

dados.loc[dados.feriado == 1]

What am I doing wrong? Could you help me? Thanks!

1 answer

2


You can do it with the following sequence:

  • Merge Dataframes with merge passing the command Indicator = True
  • Check which lines were in the 2 Dataframes with np.where
  • Delete extra column created by merge.

df= pd.merge(dados, feriados, how='left', on = 'data', indicator = True)
df['feriado'] = np.where(df['_merge'] == 'both', 1, 0)
df.drop('_merge', axis = 1, inplace= True)
  • 1

    Cara worked super well, step 3 is not even necessary because after I saw whether the date is a holiday or not has great validity in machine learning. Thanks, that tip was top!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.