Python将int添加到日期,并且仅返回工作日日期,而忽略周末

时间:2018-11-27 07:02:47

标签: python pandas dataframe

我有以下数据框

df = pd.DataFrame({'Start_Date': ['2018-08-23', '2018-08-16', '2018-08-24', '2018-08-29'], 'Days': ['3', '1', '3', '0']})

我想创建一个新列“ End_Date”。结束日期将是开始日期+天数。另外,End_Date应该只有工作日(不包括周末)。

预期

Start Date      Days   End_Date
2018-08-23      3      2018-08-28    
2018-08-16      1      2018-08-17
2018-08-24      3      2018-08-29
2018-08-29      0      2018-08-29

我尝试使用omz中的解决方案,但对我而言不起作用。我已经尝试过使用工作日功能,但这也无法正常工作。

有什么主意吗?

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式使用BDay

from IPython.utils import capture
with capture.capture_output() as cap:
## does not capture the error instead throws the given error
print abc

输出:

%%capture cap
print abc

答案 1 :(得分:0)

您可以使用numpy.busday_offset获取下一个工作日。

In [1]: import numpy as np

In [2]: df['End_Date'] = df.apply(lambda x: np.busday_offset(x[0], x[1]), axis=1)

In [3]: df
Out[3]:
   Start_Date Days   End_Date
0  2018-08-23    3 2018-08-28
1  2018-08-16    1 2018-08-17
2  2018-08-24    3 2018-08-29
3  2018-08-29    0 2018-08-29