Home > database >  Formatting a set of date columns in pandas dataframe
Formatting a set of date columns in pandas dataframe

Time:12-06

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

CodePudding user response:

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

  1. change 0020 to 2020 and 0021 to 2021
  2. use pd.to_datetime(<your_datetime>, errors = 'coerce') to represent them with NaT
  • Related