在 pandas 专栏中查找下一个工作日 [英] find next working day in a pandas column

查看:81
本文介绍了在 pandas 专栏中查找下一个工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个df熊猫,它代表着一家商店的营业日,看起来像:

I have a pandas df that represents the open days of a shop looking like:

         Dates  Open
0   2016-01-01     0
1   2016-01-02     0
2   2016-01-03     0
3   2016-01-04     1
4   2016-01-05     1
5   2016-01-06     1
6   2016-01-07     1
7   2016-01-08     1
8   2016-01-09     0
9   2016-01-10     0
10  2016-01-11     1
11  2016-01-12     1
12  2016-01-13     1
13  2016-01-14     1
14  2016-01-15     1
15  2016-01-16     0
16  2016-01-17     0
17  2016-01-18     1
18  2016-01-19     1
19  2016-01-20     1
20  2016-01-21     1
21  2016-01-22     1
22  2016-01-23     0
23  2016-01-24     0
24  2016-01-25     1
25  2016-01-26     1
26  2016-01-27     1
27  2016-01-28     1
28  2016-01-29     1

这可以通过以下方式重新创建:

this can be recreated by:

Dates =['2016-01-01',
 '2016-01-02',
 '2016-01-03',
 '2016-01-04',
 '2016-01-05',
 '2016-01-06',
 '2016-01-07',
 '2016-01-08',
 '2016-01-09',
 '2016-01-10',
 '2016-01-11',
 '2016-01-12',
 '2016-01-13',
 '2016-01-14',
 '2016-01-15',
 '2016-01-16',
 '2016-01-17',
 '2016-01-18',
 '2016-01-19',
 '2016-01-20',
 '2016-01-21',
 '2016-01-22',
 '2016-01-23',
 '2016-01-24',
 '2016-01-25',
 '2016-01-26',
 '2016-01-27',
 '2016-01-28',
 '2016-01-29']

Open = [0,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1]


df = DataFrame({'Dates':Dates, 'Open':Open})

打开的列表示开店交货的天数.我想在最左边的列中为每个日期的下一个营业日创建一个新列.我无法使用预定义的工作日功能,但必须使用打开"列来确定商店是否营业.理想的结果将是:

the column open represents the days in which the shop is open for deliveries. I want to create a new column with the next open day for each date in the leftmost column. I cannot use predefined workingdays functions but I have to use the Open column to determine whether or not the shop is open. The desired outcome would be:

         Dates  Open     Desired
0   2016-01-01     0  2016-01-04
1   2016-01-02     0  2016-01-04
2   2016-01-03     0  2016-01-04
3   2016-01-04     1  2016-01-05
4   2016-01-05     1  2016-01-06
5   2016-01-06     1  2016-01-07
6   2016-01-07     1  2016-01-08
7   2016-01-08     1  2016-01-11
8   2016-01-09     0  2016-01-11
9   2016-01-10     0  2016-01-11
10  2016-01-11     1  2016-01-12
11  2016-01-12     1  2016-01-13
12  2016-01-13     1  2016-01-14
13  2016-01-14     1  2016-01-15
14  2016-01-15     1  2016-01-18
15  2016-01-16     0  2016-01-18
16  2016-01-17     0  2016-01-18
17  2016-01-18     1  2016-01-19
18  2016-01-19     1  2016-01-20
19  2016-01-20     1  2016-01-21
20  2016-01-21     1  2016-01-22
21  2016-01-22     1  2016-01-25
22  2016-01-23     0  2016-01-25
23  2016-01-24     0  2016-01-25
24  2016-01-25     1  2016-01-26
25  2016-01-26     1  2016-01-27
26  2016-01-27     1  2016-01-28
27  2016-01-28     1  2016-01-29
28  2016-01-29     1  

推荐答案

IIUC,然后您可以添加

IIUC then you can add Business Day offset after converting your strings to datetime dtype using to_datetime:

In [141]:
df['Dates'] = pd.to_datetime(df['Dates'])
df.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 29 entries, 0 to 28
Data columns (total 2 columns):
Dates    29 non-null datetime64[ns]
Open     29 non-null int64
dtypes: datetime64[ns](1), int64(1)
memory usage: 696.0 bytes

In [146]:
from pandas.tseries.offsets import *
df['Desired'] = df['Dates'] + BDay()
df

Out[146]:
        Dates  Open    Desired
0  2016-01-01     0 2016-01-04
1  2016-01-02     0 2016-01-04
2  2016-01-03     0 2016-01-04
3  2016-01-04     1 2016-01-05
4  2016-01-05     1 2016-01-06
5  2016-01-06     1 2016-01-07
6  2016-01-07     1 2016-01-08
7  2016-01-08     1 2016-01-11
8  2016-01-09     0 2016-01-11
9  2016-01-10     0 2016-01-11
10 2016-01-11     1 2016-01-12
11 2016-01-12     1 2016-01-13
12 2016-01-13     1 2016-01-14
13 2016-01-14     1 2016-01-15
14 2016-01-15     1 2016-01-18
15 2016-01-16     0 2016-01-18
16 2016-01-17     0 2016-01-18
17 2016-01-18     1 2016-01-19
18 2016-01-19     1 2016-01-20
19 2016-01-20     1 2016-01-21
20 2016-01-21     1 2016-01-22
21 2016-01-22     1 2016-01-25
22 2016-01-23     0 2016-01-25
23 2016-01-24     0 2016-01-25
24 2016-01-25     1 2016-01-26
25 2016-01-26     1 2016-01-27
26 2016-01-27     1 2016-01-28
27 2016-01-28     1 2016-01-29
28 2016-01-29     1 2016-02-01

这篇关于在 pandas 专栏中查找下一个工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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