Skip to content Skip to sidebar Skip to footer

Changing All Time Stamps With Yyyy-mm-dd 24-mm-ss To Yyyy-mm-dd+1 00-mm-ss

Ive got a dataframe with strange hourly timestamps. It has both 00:00:00 and 24:00:00. It is as follows: TIMESTAMP RECORD 2 2021-08-01 00:01:00 85878 3 2021-08-01

Solution 1:

split date and time, parse the date to datetime and add the time as a timedelta:

import pandas as pd

# split date and time
date_time = df['TIMESTAMP'].str.split(' ', expand=True)

# parse date to datetime and time to timedelta and combine
df['TIMESTAMP'] = pd.to_datetime(date_time[0]) + pd.to_timedelta(date_time[1])

df['TIMESTAMP']
0   2021-08-01 00:01:00
1   2021-08-01 00:02:00
2   2021-08-01 00:03:00
3   2021-08-01 00:04:00
4   2021-08-01 00:05:00
5   2021-08-01 23:56:00
6   2021-08-01 23:57:00
7   2021-08-01 23:58:00
8   2021-08-01 23:59:00
9   2021-08-02 00:00:00
Name: TIMESTAMP, dtype: datetime64[ns]

Post a Comment for "Changing All Time Stamps With Yyyy-mm-dd 24-mm-ss To Yyyy-mm-dd+1 00-mm-ss"