Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Most efficient way to check if all timestamps in a Pandas DataFrame index are Business Days

I have a Pandas DataFrame. How can I check, explicitly, whether each date in the index is a business day?

import pandas as pd

df = pd.DataFrame({'col_1': [1, 2, 3, 4, 5]}, index=['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05'])

df

>>>     col_1
2020-01-01  1
2020-01-02  2
2020-01-03  3
2020-01-04  4
2020-01-05  5

I realise I could create an index using pd.bdate_range(df.index.min(), df.index.max()) and compare the lengths of the index, however, I would ideally like to create a separate column that denotes 1 if the date is a business day else 0. I could then sum over axis 0 as a test.

Are there any methods that exist to perform this, that I could apply using .map()?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you

>Solution :

Use Index.isin with casting to integers:

df['test'] = df.index.isin(pd.bdate_range(df.index.min(), df.index.max())).astype(int)
print (df)
            col_1  test
2020-01-01      1     1
2020-01-02      2     1
2020-01-03      3     1
2020-01-04      4     0
2020-01-05      5     0

Alternative with numpy.where:

df['test'] = np.where(df.index.isin(pd.bdate_range(df.index.min(), df.index.max())), 1, 0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading