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?
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.
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.
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 .
Two things:
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)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()
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With