0

I have already checked the business days in python question but wasn't able to figure out how to do the following problem.

I want to check whether a date is a business date or weekend or holiday and if it is, I would like to add days until the next business day and append a new column as in the examples provided below:

I have the following pandas dataframe:

    date    
0    11/4/17  # Saturday
1    11/8/17    
2   11/16/17    
3   11/17/17    
4   11/19/17  # Sunday
5   11/22/17    
6   11/23/17    

And then I would like to create a new column x['date_business'] with all business dates as in the following example:

    date    date_business
0    11/4/17     11/6/17
1    11/8/17     11/8/17
2   11/16/17    11/16/17
3   11/17/17    11/17/17
4   11/19/17    11/20/17
5   11/22/17    11/22/17
6   11/23/17    11/23/17
michael0196
  • 1,497
  • 1
  • 9
  • 21

1 Answers1

1

I think you need to_datetime + BDay

from pandas.tseries.offsets import *
df.date=pd.to_datetime(df.date)# convert your datetime into pandas
df['New']=np.where(df.date.dt.weekday_name.isin(['Saturday','Sunday']),df.date+BDay(1),df.date)

df
Out[1118]: 
        date        New
0 2017-11-04 2017-11-06
1 2017-11-08 2017-11-08
2 2017-11-16 2017-11-16
3 2017-11-17 2017-11-17
4 2017-11-19 2017-11-20
5 2017-11-22 2017-11-22
6 2017-11-23 2017-11-23
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Old sintax: dt.weekday_name() New sintax:.dt.day_name() from https://stackoverflow.com/questions/60214194/error-in-reading-stock-data-datetimeproperties-object-has-no-attribute-week – LuisSilva Mar 18 '21 at 22:14