Python Forum

Full Version: How to find difference between two timestamp index
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
You don't need apply Timedelta, the difference is already instance of Pandas TimeDelta class.

(df.index[-1] - df.index[0]).total_seconds()
good datetime right-up here: https://pymotw.com/3/datetime/
(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]