什么可以使 Python 的日期列过程更快



我有一个数据帧,其中包含使用时间的客户票证。

ticket_end数据not正确,我必须使用ticket_start正确的列,并且我有客户ticket_name来描述票证的长度。

我用了relativedelta(months=+numberofmonths)它正在工作,但我有 300k 行,时间超过 2 小时,所以我开始寻找其他选项,但都一样,然后我再次尝试了这段代码,只花了 5 分钟!我没有再次更改,但我不知道发生了什么,但我不得不再次启动内核,现在又需要 2 个多小时。

我的问题是我不知道为什么会发生这种情况? 以及我们可以做些什么来加快日期时间列的处理速度?

这是我的代码:

for i in tqdm(range(len(customer))):
if  customer.ticket_name[i] == '3 month free':
customer.ticket_end[i] = customer.ticket_start[i] + relativedelta(months=+1)
elif customer.product_name[i] == '4 month free':  
customer.ticket_end[i] = customer.ticket_start[i] + relativedelta(months=+4)
elif customer.product_name[i] == '6 month free':
customer.ticket_end[i] = customer.ticket_start[i] + relativedelta(months=+6)
elif customer.product_name[i] == '9 month free': 
customer.ticket_end[i] = customer.ticket_start[i] + relativedelta(months=+9)
else:
customer.ticket_end[i] = customer.ticket_start[i] + relativedelta(months=+1)

在代码之前,日期列是字符串和日期和时间'2015-01-28 17:59:50'

我不需要,所以我用这个删除了时间:

customer['ticket_start']= pd.to_datetime(customer['ticket_start'],format='%Y-%m-%d %H:%M:%S')
customer['ticket_start'] = map(lambda x: x.date(), customer['ticket_start'])

再次pd.to_datetime()

customer['ticket_start']= pd.to_datetime(customer['ticket_start'])

可能是我从csv和数据库中获取数据的关键信息mysql.connector但现在两者都是一个 2 小时的过程。

提前谢谢。

您可以使用删除时间floor,然后为months创建新列,最后按DateOffset添加它们:

rng = pd.date_range('2017-01-03  15:14:01', periods=30, freq='300H')
customer = pd.DataFrame({'ticket_start': rng, 'product_name': ['3 month free'] * 5 + 
['4 month free'] * 5 + 
['6 month free'] * 10 +
['9 month free'] * 5 +
['2 month free'] * 5} )  

#print (customer)
customer['ticket_start']=(pd.to_datetime(customer['ticket_start'],format='%Y-%m-%d %H:%M:%S')
.dt.floor('d'))
d = {'3 month free' : 1, '4 month free': 4, '6 month free':6, '9 month free':9}
customer['m'] = customer['product_name'].map(d).fillna(1).astype(int) 

customer['ticket_end'] = customer.apply(lambda x: x['ticket_start'] + 
pd.offsets.DateOffset(months=x['m']), axis=1)

print (customer)
product_name ticket_start  m ticket_end
0   3 month free   2017-01-03  1 2017-02-03
1   3 month free   2017-01-16  1 2017-02-16
2   3 month free   2017-01-28  1 2017-02-28
3   3 month free   2017-02-10  1 2017-03-10
4   3 month free   2017-02-22  1 2017-03-22
5   4 month free   2017-03-07  4 2017-07-07
6   4 month free   2017-03-19  4 2017-07-19
7   4 month free   2017-04-01  4 2017-08-01
8   4 month free   2017-04-13  4 2017-08-13
9   4 month free   2017-04-26  4 2017-08-26
10  6 month free   2017-05-08  6 2017-11-08
11  6 month free   2017-05-21  6 2017-11-21
12  6 month free   2017-06-02  6 2017-12-02
13  6 month free   2017-06-15  6 2017-12-15
14  6 month free   2017-06-27  6 2017-12-27
15  6 month free   2017-07-10  6 2018-01-10
16  6 month free   2017-07-22  6 2018-01-22
17  6 month free   2017-08-04  6 2018-02-04
18  6 month free   2017-08-16  6 2018-02-16
19  6 month free   2017-08-29  6 2018-02-28
20  9 month free   2017-09-10  9 2018-06-10
21  9 month free   2017-09-23  9 2018-06-23
22  9 month free   2017-10-05  9 2018-07-05
23  9 month free   2017-10-18  9 2018-07-18
24  9 month free   2017-10-30  9 2018-07-30
25  2 month free   2017-11-12  1 2017-12-12
26  2 month free   2017-11-24  1 2017-12-24
27  2 month free   2017-12-07  1 2018-01-07
28  2 month free   2017-12-19  1 2018-01-19
29  2 month free   2018-01-01  1 2018-02-01

最新更新