问题
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