Importing datetimes to pandas DataFrame raises OutOfBoundsDatetime error

一世执手 提交于 2021-02-16 21:27:18

问题


I'm trying to import data to pandas DataFrame, but getting the following error while trying to convert the date_time column to datetime object:

pandas.tslib.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-19 00:00:00

The format of the column looks like: Jan 19,17 05:04:50 PM

My code is:

data['Date_Time'] = to_datetime(data['Date_Time']).dt.strftime('%b %d, %y %H:%M:%S ')

What is the problem?


回答1:


I think you need to_datetime with parameter format:

data = pd.DataFrame({'Date_Time':['Jan 19,17 05:04:50 PM','Jan 19,17 05:04:50 PM']})
print (data)
               Date_Time
0  Jan 19,17 05:04:50 PM
1  Jan 19,17 05:04:50 PM

data['Date_Time'] = pd.to_datetime(data['Date_Time'], format='%b %d,%y %H:%M:%S %p')
print (data)
            Date_Time
0 2017-01-19 05:04:50
1 2017-01-19 05:04:50



回答2:


I fixed it by using regex - replaced bad values.

And then using pandas to_datetime function with parameter format.



来源:https://stackoverflow.com/questions/42107377/importing-datetimes-to-pandas-dataframe-raises-outofboundsdatetime-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!