计算两个系列之间的工作日 [英] Counting the business days between two series

查看:70
本文介绍了计算两个系列之间的工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有比bdate_range()更好的方法来通过熊猫测量两列日期之间的营业日?

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

输出为:

          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

谢谢!

推荐答案

brian_the_bungler使用了numpy的busday_count实现此目的的最有效方法:

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

在我的机器上,这比测试用例快300倍,而对大得多的日期数组,则快1000倍

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

这篇关于计算两个系列之间的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆