简体   繁体   中英

Formatting a set of date columns in pandas dataframe

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM