python字符串转datetime,将datetime字符串转换为datetime(python)

I would like to convert

['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"]

into a Numpy datetime object.

import numpy as np

[np.datetime64(x) for x in ['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"]]

raised ValueError: Could not convert object to NumPy datetime. However, the following works as I intended

[np.datetime64(x) for x in ['2010-10-17 07:15:30', '2011-05-13 08:20:35', "2012-01-15 09:09:09"]]

How can I convert my array into a format that conforms with Numpy's datetime64 function requirement?

I am using Numpy version 1.7.0. in python 3.4

解决方案

So far as I can tell, np.datetime64 only works with

The to_datetime function in pandas seems to be more flexible:

import pandas as pd

a=pd.to_datetime(['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"])

Of course you can easily convert back to numpy:

np.array(a,dtype=np.datetime64)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值