bin
bin

Reputation: 39

Importing datetimes to pandas DataFrame raises OutOfBoundsDatetime error

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?

Upvotes: 2

Views: 8212

Answers (2)

bin
bin

Reputation: 39

I fixed it by using regex - replaced bad values.

And then using pandas to_datetime function with parameter format.

Upvotes: -2

jezrael
jezrael

Reputation: 863166

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

Upvotes: 3

Related Questions