Python Forum
Pandas datetime: add timedelta - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Pandas datetime: add timedelta (/thread-32597.html)



Pandas datetime: add timedelta - ju21878436312 - Feb-20-2021

Hey out there,

Question 1:

Hi there, I've created timedeltas

df['Time'] = df['Time'] - df.Time.min()
and would now like to add a time increment of "57 s" to each line in the Time series.

I have experimenced with

df['Time'] = df['Time'] + df.seconds(57)
but this does not work. Can someone help me?

Question 2:

Furthermore, I would like plot column "0.3" over "Time". My attempts using matplotlib were not successfull by now.

Question 3:

Can I determine the Time, at which "0.3" is above or below a certain value for the first time?

minimal_datetime.ipynb:

import pandas as pd
from datetime import datetime, timedelta

df = pd.read_csv('minimal_in.txt')               
df.head(5)

# overwrite Time to combine Time and Year
df['Time'] = df['Year']+ ", "+ df['Time']
df

# change to datetime
df['Time']=pd.to_datetime(df.Time)
df.head()

df.dtypes

# Creation of timedeltas
df['Time'] = df['Time'] - df.Time.min()
#df['Time'] = df['Time'] + df.seconds(57)
df

# Write csv
df.to_csv('minimal_out.csv', index=False)
minimal_in.txt:

,Year,Time,Channel,time,0.3,0.5,1.0,3.0,5.0,10.0
0,29.01.2001,15:22:36,1,2,39,9,0,0,0,0
1,29.01.2001,15:22:39,1,2,622,243,66,1,0,0
2,29.01.2001,15:22:41,1,2,803,311,57,0,0,0
3,29.01.2001,15:22:44,1,2,2826,1217,352,1,0,0
4,29.01.2001,15:22:46,1,2,3282,1487,405,5,0,0
minimal_out.csv:
,Unnamed: 0,Year,Time,Channel,time,0.3,0.5,1.0,3.0,5.0,10.0
0,29.01.2001,0 days 00:00:00.000000000,1,2,39,9,0,0,0,0
1,29.01.2001,0 days 00:00:03.000000000,1,2,622,243,66,1,0,0
2,29.01.2001,0 days 00:00:05.000000000,1,2,803,311,57,0,0,0
3,29.01.2001,0 days 00:00:08.000000000,1,2,2826,1217,352,1,0,0
4,29.01.2001,0 days 00:00:10.000000000,1,2,3282,1487,405,5,0,0
Thanks in advance!


RE: Pandas datetime: add timedelta - eddywinch82 - Feb-25-2021

Hi ju21878436312,

As regards the first question, try the following :-

df['Time'] = df['Time'] + datetime.timedelta(seconds=57)
Or if that doesn't work.

Try :-

df['Time'] = df['Time'] + df.timedelta(seconds=57)
Or :-

df['Time'] = df['Time'] + timedelta(seconds=57)
I hope that works for you.

I may be able to help you with question 3, I will have a think.

Regards

Eddie Winch


RE: Pandas datetime: add timedelta - ju21878436312 - Jun-23-2021

Hi there, sorry for the late reply.
The attempts did non work, as the columns were not interpreted as datetimes. I solved it this way:


df = pd.read_csv('minimal_in.txt', delimiter= '\t',parse_dates=[[0, 1]], header=None, names=["Date","Time","Channel","time","0.3","0.5","1.0","3.0","5.0","10.0"])
df['Date_Time'] = df['Date_Time'] + pd.Timedelta(seconds = 57)  



RE: Pandas datetime: add timedelta - eddywinch82 - Jul-05-2021

Hi ju21878436312,

I am glad, you have found an solution, to the issue you were having with your Python Code, I am sorry my suggestions didn't work for you.

Best Regards

Eddie Winch Smile