Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating time difference between two rows

Tags:

python

pandas

I'm trying to calculate the time difference between two rows using shift(), but I get an unexpected error. I may be missing something obvious

df['Delta'] = (df.index - df.index.shift(1))

This statement produces a ValueError: Cannot shift with no offset. What am I missing?

like image 987
Paal Avatar asked Aug 15 '14 14:08

Paal


People also ask

How do I compare two consecutive rows in SQL?

Here's the SQL query to compare each row with previous row. In the above query, we join sales table with itself using an INNER JOIN condition g2.id=g1.id + 1 that allows you to compare each row with its previous row. Please note, this condition depends on the fact that our id column has consecutive numbers.

How do I find the difference between two values in SQL?

SQL Server DIFFERENCE() Function The DIFFERENCE() function compares two SOUNDEX values, and returns an integer. The integer value indicates the match for the two SOUNDEX values, from 0 to 4. 0 indicates weak or no similarity between the SOUNDEX values. 4 indicates strong similarity or identically SOUNDEX values.

How can I find the difference between two timestamps in SQL?

To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR .


2 Answers

Two things:

  • If you have a DatetimeIndex, the shift shifts your data with a period of time. If your index has no frequency, you have to provide that to the shift method with the freq keyword (eg freq='s' to shift the data one second)
  • You cannot substract two index objects like that, as this gives you a difference set operation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#set-operations-on-index-objects

If you just want the difference between two consecutive values in the index, you can use the diff method (of a Series, a bit easier than shift and substract):

df['index_col'] = df.index
df['Delta'] = df['index_col'].diff()
like image 137
joris Avatar answered Oct 09 '22 03:10

joris


Perhaps confusingly, pre-1.0 Series.shift and Index.shift used to not exactly do the same thing, the latter only being meaningfully defined for TimesSeries. Probably easiest to add your index as a column.

df['index_col'] = df.index
df['Delta']=(df['index_col'] - df['index_col'].shift(1))
like image 20
chrisb Avatar answered Oct 09 '22 04:10

chrisb