python_把字符串转化为日期

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/
    原文作者:六mo神剑
    原文地址: https://blog.csdn.net/wj1298250240/article/details/103755469
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞