Martan
Martan

Reputation: 605

pandas to_datetime does not accept '24' as time

The time is in the YYYYMMDDHH format.The first time 2010010101, increases by 1 hour, reaches 2010010124, then 2010010201.

    date
0   2010010101
1   2010010124
2   2010010201

df['date'] = pd.to_datetime(df['date'], format ='%Y%m%d%H')

I am getting error:

'int' object is unsliceable

If I run:

df2['date'] = pd.to_datetime(df2['date'], format ='%Y%m%d%H', errors = 'coerce')

All the '24' hour is labeled as NaT.

[before[1]

after

Upvotes: 1

Views: 1286

Answers (1)

Vaishali
Vaishali

Reputation: 38415

Time starts from 00 (midnight) till 23 so the time 24 in your date is 00 of the next day. One way is to define a custom to_datetime to handle the date format.

df = pd.DataFrame({'date':['2010010101', '2010010124', '2010010201']})

def custom_to_datetime(date):
    # If the time is 24, set it to 0 and increment day by 1
    if date[8:10] == '24':
        return pd.to_datetime(date[:-2], format = '%Y%m%d') + pd.Timedelta(days=1)
    else:
        return pd.to_datetime(date, format = '%Y%m%d%H')

df['date'] = df['date'].apply(custom_to_datetime)  


    date
0   2010-01-01 01:00:00
1   2010-01-02 00:00:00
2   2010-01-02 01:00:00

Upvotes: 1

Related Questions