我有一个这种格式的日期 - '17-JUL-53'
当我 pd.to_datetime('17-JUL-53')
它返回 Timestamp('2053-07-17 00:00:00')
你可以说它是正确的,但实际返回的日期是 1953-07-17。在 excel 中结果正常,我们如何使用 to_datetime() 做到这一点?
[编辑] 只是为了展示当我们在 python 中将 str 转换为 time 时会发生什么:
>>> time.strptime('17-JUL-53', '%d-%b-%y')
time.struct_time(tm_year=2053, tm_mon=7, tm_mday=17, tm_hour=0, tm_min=0,tm_sec=0, tm_wday=3, tm_yday=198, tm_isdst=-1)
最佳答案
我认为您需要将子字符串 19
添加到年份。
有关日期时间格式的更多信息是 here .
import pandas as pd
s = '17-JUL-53'
d = s[:7] + '19' + s[7:]
print d
#17-JUL-1953
dt = pd.to_datetime(d, format='%d-%b-%Y')
print dt
#1953-07-17 00:00:00
%d-%b-%Y
表示:
%d - Day of the month as a zero-padded decimal number
%b - Month as locale’s abbreviated name
%Y - Year with century as a decimal number
关于python - Pandas dataframe to_datetime() 转换日期不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33646808/