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()?

Thank you

1 answer

  • answered 2022-05-04 10:36 jezrael

    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)
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum