python_把字符串转化为日期
import numpy as np
import pandas as pd
# 创建字符串
date_strings = np.array([
'03-04-2005 11:35 PM',
'23-05-2010 12:01 AM',
'04-09-2009 09:09 PM'
])
# 转换为datatime类型的数据
# convert to datetimes 循环
[pd.to_datetime(date, format='%d-%m-%Y %I:%M %p') for date in date_strings]
[Timestamp('2005-04-03 23:35:00'),
Timestamp('2010-05-23 00:01:00'),
Timestamp('2009-09-04 21:09:00')]
# 遇到不能转换的数据就会赋值为 NaN(Not a Number)
[pd.to_datetime(date, format='%d-%m-%Y %I:%M %p', errors='coerce') for date in date_strings]
[Timestamp('2005-04-03 23:35:00'),
Timestamp('2010-05-23 00:01:00'),
Timestamp('2009-09-04 21:09:00')]
See Also
http://strftime.org/