I have a csv file with 24:00 hour instead of 00:00 and try to read it with pandas. I found solution and try to adopt it. The problem is, I get an error and don’t know how to fix it. Can someone help me?
My csv:
JavaScript
x
8
1
Datetime Value
2
45 01.01.2021 23:00 2.7
3
46 01.01.2021 23:30 2.9
4
47 01.01.2021 24:00 1.5
5
48 02.01.2021 00:30 1.2
6
49 02.01.2021 01:00 1.9
7
50 02.01.2021 01:30 1.9
8
The code I got from the link above adopted to my case:
JavaScript
1
3
1
df['Datetime'] = (pd.to_datetime(df['Datetime'].str[:10], format='%d.%m.%Y') +
2
pd.to_timedelta(df['Datetime'].str[10:12]+':'+df['Datetime'].str[12:14]+':00'))
3
The error I get:
JavaScript
1
2
1
expecting hh:mm:ss format, received: 0:0::00
2
The link where I got the function to transform it:
Pandas: parsing 24:00 instead of 00:00
Advertisement
Answer
You can use str.split()
+pd.to_datetime()
+pd.to_timedelta()
:
JavaScript
1
3
1
s=df['Datetime'].str.replace('.','-').str.split(expand=True)
2
df['Datetime']=pd.to_datetime(s[0])+pd.to_timedelta(s[1]+':00')
3
OR
JavaScript
1
2
1
df['Datetime']=pd.to_datetime(df['Datetime'].str[:10], format='%d.%m.%Y')+pd.to_timedelta(df['Datetime'].str[10:]+':00')
2
output of df
:
JavaScript
1
8
1
Datetime Value
2
45 2021-01-01 23:00:00 2.7
3
46 2021-01-01 23:30:00 2.9
4
47 2021-01-02 00:00:00 1.5
5
48 2021-02-01 00:30:00 1.2
6
49 2021-02-01 01:00:00 1.9
7
50 2021-02-01 01:30:00 1.9
8