【发布时间】:2020-06-27 22:46:19
【问题描述】:
我正在尝试将字符串格式的不同日期转换为日期时间格式。我在“status_change_date”列中有大约 1,000,000 行日期。问题是有许多不同的格式,我不知道它们都是什么格式。
所以,我正在我的 jupyter 笔记上尝试这个功能:
def parsing_date(date_string):
for date_format in ("%d/%m/%Y %H:%M:%S", "%m/%d/%Y %H:%M:%S", "%m/%d/%Y %H:%M:%S%p", "%d/%m/%Y %H:%M:%S%p", "%-m/%-d/%Y %H:%M:%S", "%-d/%-m/%Y %H:%M:%S", "%-m/%-d/%Y %H:%M:%S%p", "%-d/%-m/%Y %H:%M:%S%p"):
try:
return datetime.strptime(date_string, date_format)
except ValueError:
raise
print(date_string)
raise ValueError('Something is wrong')
那么,
data['status_chage_date'].apply(parsing_date)
我正在打印错误,所以我可以在我的函数中一一考虑,直到不再有错误为止。但是,我遇到此错误消息:
ValueError: time data '17/10/2019 05:49:51' does not match format '%m/%d/%Y %H:%M:%S'
或
strptime() argument 1 must be str, not None
我以为我在函数中涵盖了格式,第二个错误很奇怪,因为我排除了具有 None 值的行。
我做错了什么,有更好的方法吗?
【问题讨论】:
标签: python pandas date datetime