Issue
I have the below date formats as object type when I am trying to convert into date , it is giving me outOfBoundsDateTime error and I am not able to convert it into correct format.
date_1 | date_2 | date_3 |
---|---|---|
0020-01-31 | 0020-01-31 | 2020-01-31 |
0021-01-01 | 0021-12-31 | 2021-02-28 |
0021-01-01 | 0021-12-31 | 2021-02-28 |
Here is the tried code:
import pandas as pd
for column in my_df[['date_1', 'date_2','date_3']]:
new_column=pd.to_datetime(global_events_df[column]).dt.date
Error :
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 20-01-31 00:00:00
anyone faced this similar issue, please guide
Solution
0020-01-31
exceeds the range of the pandas datetime supported by a 64-bit integer.
Belows are the max and min value of datetime that pandas supports:
>>> pd.Timestamp.max
Timestamp('2262-04-11 23:47:16.854775807')
>>> pd.Timestamp.min
Timestamp('1677-09-21 00:12:43.145224193')
So, your options are
- change
0020
to2020
and0021
to2021
- use
pd.to_datetime(<your_datetime>, errors = 'coerce')
to represent them withNaT
Answered By - JayPeerachai
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.