Python Forum
How to find difference between two timestamp index
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find difference between two timestamp index
#1
I have a dataframe with the date time as the index and I would like to know how to get the difference in days between the first time stamp and the last timestamp.

i tried the following code:

df.index[0]
Timestamp('2013-09-10 13:45:36.999997')

df.index[-1]
Timestamp('2014-06-18 07:53:05.999996')

diff = pd.Timedelta(df.index[-1] - df.index[0]).seconds #try for seconds
error: ValueError: Cannot add integral value to Timestamp without freq.
Reply
#2
You don't need apply Timedelta, the difference is already instance of Pandas TimeDelta class.

(df.index[-1] - df.index[0]).total_seconds()
Reply
#3
good datetime right-up here: https://pymotw.com/3/datetime/
Reply
#4
(Jul-25-2019, 12:20 AM)scidam Wrote: You don't need apply Timedelta, the difference is already instance of Pandas TimeDelta class.

(df.index[-1] - df.index[0]).total_seconds()

thank you. this worked. I added dividing by 86400 to get the days as well.

((df.index[-1] - df.index[0]).total_seconds())/86400
[/quote]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Getting Index Error - list index out of range krishna 2 2,625 Jan-09-2021, 08:29 AM
Last Post: buran
  Getting Index Error - list index out of range RahulSingh 2 6,178 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  How to find index of a particular value in a dataframe ankitawadhwa 0 2,393 Jan-21-2020, 09:45 PM
Last Post: ankitawadhwa
  How to find column index and its corresponding column name Raj 2 3,425 May-10-2018, 12:30 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

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