将工作日添加到日期时间列 [英] Adding Business days to datetime column

查看:56
本文介绍了将工作日添加到日期时间列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将工作日添加到数据框中现有列中列出的日期.列中的数据为datetime64 [ns]类型.使用 Python中的工作日中的建议,我尝试了下面列出的代码.

I need to add business days to the dates listed in an existing column in a dataframe. The data in the column is in the datetime64[ns] type. Using the suggestions from Business days in Python, I tried the code listed below.

import datetime
from pandas.tseries.offsets import BDay

df['Math Admin Date'] + BDay(1)

我收到以下错误消息:

TypeError: cannot use a non-absolute DateOffset in datetime/timedelta operations [<BusinessDay>]

如何在日期时间"列中添加工作日?

How can I add business days to my datetime column?

推荐答案

不幸的是,偏移量不支持使用类似数组的对象的操作,因此您必须apply每个元素的偏移量:

Unfortunately offsets don't support operations using array like objects so you have to apply the offset for each element:

In [208]:
import datetime as dt
from pandas.tseries.offsets import BDay
​
df = pd.DataFrame({'Math Admin Date':pd.date_range(start=dt.datetime(2015,6,1), end = dt.datetime(2015,6,30))})
df['Math Admin Date'].apply(lambda x: x + BDay(1))

Out[208]:
0    2015-06-02
1    2015-06-03
2    2015-06-04
3    2015-06-05
4    2015-06-08
5    2015-06-08
6    2015-06-08
7    2015-06-09
8    2015-06-10
9    2015-06-11
10   2015-06-12
11   2015-06-15
12   2015-06-15
13   2015-06-15
14   2015-06-16
15   2015-06-17
16   2015-06-18
17   2015-06-19
18   2015-06-22
19   2015-06-22
20   2015-06-22
21   2015-06-23
22   2015-06-24
23   2015-06-25
24   2015-06-26
25   2015-06-29
26   2015-06-29
27   2015-06-29
28   2015-06-30
29   2015-07-01
Name: Math Admin Date, dtype: datetime64[ns]

可以使用Timedelta,但这暂时不支持工作日:

Using a Timedelta would work but this doesn't support business days at the moment:

df['Math Admin Date'] + pd.Timedelta(1,'D')

这篇关于将工作日添加到日期时间列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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