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
d
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()