使用 pandas 获取开始日期和结束日期之间的工作日 [英] Get business days between start and end date using pandas
本文介绍了使用 pandas 获取开始日期和结束日期之间的工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用熊猫,我想知道使用熊猫在开始日期和结束日期之间获取工作日的最简单方法是什么?
I'm using pandas and I'm wondering what's the easiest way to get the business days between a start and end date using pandas?
关于在Python中执行此操作的文章很多(例如示例) ,但我会对直接使用熊猫感兴趣,因为我认为熊猫可能可以轻松解决这一问题.
There are a lot of posts out there regarding doing this in Python (for example), but I would be interested to use directly pandas as I think that pandas can probably handle this quite easy.
推荐答案
使用BDay()
获取范围内的工作日.
Use BDay()
to get the business days in range.
from pandas.tseries.offsets import *
In [185]: s
Out[185]:
2011-01-01 -0.011629
2011-01-02 -0.089666
2011-01-03 -1.314430
2011-01-04 -1.867307
2011-01-05 0.779609
2011-01-06 0.588950
2011-01-07 -2.505803
2011-01-08 0.800262
2011-01-09 0.376406
2011-01-10 -0.469988
Freq: D
In [186]: s.asfreq(BDay())
Out[186]:
2011-01-03 -1.314430
2011-01-04 -1.867307
2011-01-05 0.779609
2011-01-06 0.588950
2011-01-07 -2.505803
2011-01-10 -0.469988
Freq: B
带切片:
In [187]: x=datetime(2011, 1, 5)
In [188]: y=datetime(2011, 1, 9)
In [189]: s.ix[x:y]
Out[189]:
2011-01-05 0.779609
2011-01-06 0.588950
2011-01-07 -2.505803
2011-01-08 0.800262
2011-01-09 0.376406
Freq: D
In [190]: s.ix[x:y].asfreq(BDay())
Out[190]:
2011-01-05 0.779609
2011-01-06 0.588950
2011-01-07 -2.505803
Freq: B
和count()
In [191]: s.ix[x:y].asfreq(BDay()).count()
Out[191]: 3
这篇关于使用 pandas 获取开始日期和结束日期之间的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文