How to add a start datetime to elapsed time

Question:

I have CSV file with temperature and elapsed time (seconds):

el_time;temp_c
0;5.974
3600;6.015
7200;6.153

I also know the start time (dd.mm.yyyy hh:mm:ss):

start_time = 20.01.2023 20:05:33

How to plot the graph using Pandas dataframe?
So far I have come up with the following, but of course its not working.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('temperature.csv', sep=';')

start_date ='2023-01-20T20:05:33'
start_date_object = datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S')

x = start_date_object +  df['el_time'].values
y = df['temp_c'].values

plt.plot(x, y)
Asked By: fred

||

Answers:

You can convert the string datetime to datetime type with pd.to_datetime and convert the seconds to timedelta with pd.to_timedelta

start_time = '20.01.2023 20:05:33'

x = pd.to_datetime(start_time, dayfirst=True) + pd.to_timedelta(df['el_time'], unit='S')
y = df['temp_c']

plt.plot(x, y)
plt.xticks(rotation=30)
plt.show()

enter image description here

Answered By: Ynjxsjmh