Python Forum
How to find difference between two timestamp index - 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: How to find difference between two timestamp index (/thread-20015.html)



How to find difference between two timestamp index - python_newbie09 - Jul-24-2019

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.


RE: How to find difference between two timestamp index - scidam - Jul-25-2019

You don't need apply Timedelta, the difference is already instance of Pandas TimeDelta class.

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



RE: How to find difference between two timestamp index - Larz60+ - Jul-25-2019

good datetime right-up here: https://pymotw.com/3/datetime/


RE: How to find difference between two timestamp index - python_newbie09 - Aug-01-2019

(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]