Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the business days between two series

Tags:

Is there a better way than bdate_range() to measure business days between two columns of dates via pandas?

df = pd.DataFrame({ 'A' : ['1/1/2013', '2/2/2013', '3/3/2013'],
 'B': ['1/12/2013', '4/4/2013', '3/3/2013']})
print df
df['A'] = pd.to_datetime(df['A'])
df['B'] = pd.to_datetime(df['B'])
f = lambda x: len(pd.bdate_range(x['A'], x['B']))
df['DIFF'] = df.apply(f, axis=1)
print df

With output of:

          A          B
0  1/1/2013  1/12/2013
1  2/2/2013   4/4/2013
2  3/3/2013   3/3/2013
                    A                   B  DIFF
0 2013-01-01 00:00:00 2013-01-12 00:00:00     9
1 2013-02-02 00:00:00 2013-04-04 00:00:00    44
2 2013-03-03 00:00:00 2013-03-03 00:00:00     0

Thanks!

like image 554
brian_the_bungler Avatar asked Jul 17 '13 15:07

brian_the_bungler


People also ask

How to calculate the number of business days between two dates?

How to determine the number of business days between two dates 1. The end date must be greater than the start date else then -99. 2. The calculation will not function correctly if either the start or end date is a weekend. Final condition: there are some number of weeks between the dates _days_between (date1, date2) will return the number of days.

How to calculate total business days in Excel using networkdays?

The Excel function NETWORKDAYS() uses the start date and end date inclusively when calculating the total business days. So for row 3 the end date of 3/8/2017 counts as a business day as well.

How do I Count days between two dates in Excel?

Home › Resources › Excel Resources › Functions › NETWORKDAYS Function. The NETWORKDAYS Function calculates the number of workdays between two dates in Excel. When using the function, the number of weekends are automatically excluded. It also allows you to skip specified holidays and only count business days.

What are the conditions for calculating the start and end dates?

1. The end date must be greater than the start date else then -99. 2. The calculation will not function correctly if either the start or end date is a weekend. Final condition: there are some number of weeks between the dates


1 Answers

brian_the_bungler was onto the most efficient way of doing this using numpy's busday_count:

import numpy as np
A = [d.date() for d in df['A']]
B = [d.date() for d in df['B']]
df['DIFF'] = np.busday_count(A, B)
print df

On my machine this is 300x faster on your test case, and 1000s of times faster on much larger arrays of dates

like image 177
Antonbass Avatar answered Oct 03 '22 23:10

Antonbass