Home > Back-end >  add business days to a df column
add business days to a df column

Time:10-07

I want to add a column called 'Date' which starts from todays date and adds business days as you go down the df up until a year. I am trying the below code but it repeats days as its adding a BD to Friday and Saturdays. The output should have row 1 = 2021-10-07 and end with 2022-10-08 with only BD being shown. Can anyone help please?

import datetime as dt
from pandas.tseries.offsets import BDay
from datetime import date

df = pd.DataFrame({'Date': pd.date_range(start=date.today(), end=date.today()   dt.timedelta(days=365))})
df['Date'] = df['Date']   BDay(1)

CodePudding user response:

It is unclear what your desired output is, but if you want a column 'Date' that only shows the dates for business days, you can use the code below.

import datetime as dt
import pandas as pd
from datetime import date

df = pd.DataFrame({'Date': pd.date_range(start=date.today(), end=date.today()   dt.timedelta(days=365))})
df = df[df.Date.dt.weekday < 5] # 0 is Monday, # 6 is Sunday
  • Related