1

I am working on a project and I am trying to calculate the number of business days within a month. What I currently did was extract all of the unique months from one dataframe into a different dataframe and created a second column with

df2['Signin Date Shifted'] = df2['Signin Date'] + pd.DateOffset(months=1)

Thus the current dataframe looks like:

enter image description here

I know I can do dt.daysinmonth or a timedelta but that gives me all of the days within a month including Sundays/Saturdays (which I don't want).

scarecrow
  • 84
  • 13
  • https://stackoverflow.com/questions/13019719/get-business-days-between-start-and-end-date-using-pandas does this answer your question? – harvpan Apr 26 '18 at 17:43
  • @HarvIpan So expand what I have above into days and then filter out all weekends and then count it? – scarecrow Apr 26 '18 at 17:49

1 Answers1

4

Using busday_count from np

Ex:

import pandas as pd
import numpy as np

df = pd.DataFrame({"Signin Date": ["2018-01-01", "2018-02-01"]})
df["Signin Date"] = pd.to_datetime(df["Signin Date"])
df['Signin Date Shifted'] = pd.DatetimeIndex(df['Signin Date']) + pd.DateOffset(months=1)

df["bussDays"] = np.busday_count( df["Signin Date"].values.astype('datetime64[D]'), df['Signin Date Shifted'].values.astype('datetime64[D]'))
print(df)

Output:

  Signin Date Signin Date Shifted  bussDays
0  2018-01-01          2018-02-01        23
1  2018-02-01          2018-03-01        20

MoreInfo

Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • Hey rakesh thanks so much! I looked at busday_count but I never realized I could use a dataframe like that. – scarecrow Apr 26 '18 at 18:16