python 文本转日期,将文本转换为numpy中的datetime64

这篇博客探讨了如何将包含日期的numpy字符串数组转换为datetime64,重点在于提高效率。通过重新排列和解析数据,而不是使用strptime,可以显著减少处理时间。文章还提供了性能比较,显示这种方法比直接使用strptime快得多。
摘要由CSDN通过智能技术生成

I have numpy array of strings (p.s. why is string represented as object?!)

t = array(['21/02/2014 08:40:00 AM', '11/02/2014 10:50:00 PM',

'07/04/2014 05:50:00 PM', '17/02/2014 10:20:00 PM',

'07/03/2014 06:10:00 AM', '02/03/2014 12:25:00 PM',

'05/02/2014 03:20:00 AM', '31/01/2014 12:30:00 AM',

'28/02/2014 01:25:00 PM'], dtype=object)

I would like to convert it to numpy.datetime64 with day resolution, however the only solution I found is:

t = [datetime.strptime(tt,"%d/%m/%Y %H:%M:%S %p") for tt in t]

t = np.array(t,dtype='datetime64[us]').astype('datetime64[D]')

Can it get uglier than that? Why do I need to go through native Python list?

There must be another way...

By the way, I cannot find a way

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值