2

I have the following column in a dataframe, I would like to add a column to the end of this dataframe, where the column has the business days from today (6/24) to the previous day.

Bday() function does not seem to have this capability.

Date
2019-6-21
2019-6-20
2019-6-14

I am looking for a result that looks like following:

Date         Business days
2019-6-21      1
2019-6-20      2
2019-6-14      6

Is there an easy way to do this, other than doing individual manipulations or using datetime library

cs95
  • 379,657
  • 97
  • 704
  • 746
Jesh Kundem
  • 960
  • 3
  • 18
  • 30

1 Answers1

2

Use np.busday_count:

# df['Date'] = pd.to_datetime(df['Date']) # if needed
np.busday_count(df['Date'].dt.date, np.datetime64('today'))
# array([1, 2, 6])

df['bdays'] = np.busday_count(df['Date'].dt.date, np.datetime64('today'))
df
        Date  bdays
0 2019-06-21      1
1 2019-06-20      2
2 2019-06-14      6
cs95
  • 379,657
  • 97
  • 704
  • 746
  • It worked, but I got this following error: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead – Jesh Kundem Jun 24 '19 at 17:24
  • @JeshKundem It's not an error, it's a warning. I need you to read [this](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas/53954986#53954986) carefully, and then add `df = df.copy()` at the top of this code. – cs95 Jun 24 '19 at 17:25