Search code examples
pythonpandasdatetime-seriesfrequency

Is there a way to make frequency include business days but without (US) holidays in Python?


I'm trying to generate a pandas.DataFrame with all the 'trading days' in 2017, essentially business days minus holidays. Alternatively, I could make a list (from another pandas.DataFrame, called 'dates' for arguments sake.) and pass that but not quite sure how.

Below is code of what I am currently generating, but holidays are included.

new_fut = pd.DataFrame(pd.date_range(start = '2017-1-1', end = '2018-1-1',  freq = 'b'), columns = ['ds'])

Solution

  • Here you go. this will take out all the holidays from Custom Business Day.

    import pandas as pd
    from pandas.tseries.holiday import USFederalHolidayCalendar
    from pandas.tseries.offsets import CustomBusinessDay
    start ='2017-1-1'
    end = '2018-1-1'
    us_bd = CustomBusinessDay(calendar=USFederalHolidayCalendar())
    df=pd.DatetimeIndex(start=start,end=end, freq=us_bd)
    
    df