Python Forum
Pandas datetime: add timedelta
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas datetime: add timedelta
#1
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!
Reply
#2
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
ju21878436312 likes this post
Reply
#3
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)  
Reply
#4
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
ju21878436312 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with timedelta error, trying to sum alkaline3 0 1,358 Mar-22-2020, 04:25 PM
Last Post: alkaline3
  itertuples, datetime, pandas, groupby, in range karlito 0 2,468 Nov-29-2019, 11:35 AM
Last Post: karlito
  itertuples, new column, datetime, pandas karlito 6 3,958 Nov-29-2019, 11:07 AM
Last Post: karlito
  Python Pandas datetime nuncio 3 10,187 Jan-08-2017, 09:34 AM
Last Post: nuncio

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020