Skip to content
Advertisement

Problem combining Date and Time Column using python pandas

First of all: I’m sorry if that’s a repost, but I did not found a similiar question.

I have a DataFrame containing one column with values, one with timestamps and one with a corresponding date.

JavaScript

As far as I understood I have to use pd.to_datetime for column ‘Date’ and pd.to_timedelta for column ‘Time’.

My problem is: I want to plot this whole DataFrame. Therefore I have to combine both columns into one, named ‘Date_Time’. That worked so far, but problems occurs on rows where the df[‘Time’] is 24:00:00. And I got the Errormessage, that my Time has to be between 0 and 23.

Both columns contain strings so far. I thought about replacing the 24:00:00 by 00:00:00 and so the corresponding date has to change to the next day.
But I don’t know how to do that.

My desired Output should look like:

JavaScript

Thanks in advance.

Advertisement

Answer

If you want a string, use:

JavaScript

output:

JavaScript

To correctly handle the 24:00 as dateimt:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement