使用熊猫创建Paydays的DateOffset
问题描述:
我试图使用Pandas在Python中创建一个时间索引,其中包含对应于周期性发薪日的条目。具体来说,我希望索引对应于本月的第一个和第三个星期五。有人可以给出一个代码片段来证明这一点吗?使用熊猫创建Paydays的DateOffset
喜欢的东西:
import pandas as pd
idx = pd.date_range("2016-10-10", periods=26, freq=<offset here?>)
答
试试这个:
In [6]: pd.date_range("2016-10-10", periods=26, freq='WOM-1FRI').union(pd.date_range("2016-10-10", periods=26, freq='WOM-3FRI'))
Out[6]:
DatetimeIndex(['2016-10-21', '2016-11-04', '2016-11-18', '2016-12-02', '2016-12-16', '2017-01-06', '2017-01-20', '2017-02-03', '2017-02-17',
'2017-03-03', '2017-03-17', '2017-04-07', '2017-04-21',
'2017-05-05', '2017-05-19', '2017-06-02', '2017-06-16', '2017-07-07', '2017-07-21', '2017-08-04', '2017-08-18', '2017-09-01',
'2017-09-15', '2017-10-06', '2017-10-20', '2017-11-03',
'2017-11-17', '2017-12-01', '2017-12-15', '2018-01-05', '2018-01-19', '2018-02-02', '2018-02-16', '2018-03-02', '2018-03-16',
'2018-04-06', '2018-04-20', '2018-05-04', '2018-05-18',
'2018-06-01', '2018-06-15', '2018-07-06', '2018-07-20', '2018-08-03', '2018-08-17', '2018-09-07', '2018-09-21', '2018-10-05',
'2018-10-19', '2018-11-02', '2018-11-16', '2018-12-07'],
dtype='datetime64[ns]', freq=None)
+0
光滑,这工作。谢谢! – themaestro
+0
@themaestro,欢迎您:) – MaxU
我不知道大熊猫有这种内置的功能。您可能必须使用'dateutil'来构建一系列日期时间对象。请参阅:http://*.com/questions/28680896/how-can-i-get-the-3rd-friday-of-a-month-in-python –