Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time difference between indices with pandas

I would like to compute the difference between consecutive data row's timestamps.

Why does this work, as explained here :

df['tvalue'] = df.index
print df['tvalue'].shift() - df['tvalue']

but not

print df.index.shift() - df.index

which produces ValueError: Cannot shift with no freq ?

The problem is that the working solution doubles the memory size needed for timestamp; this is a problem for huge dataframes.

like image 433
Basj Avatar asked Nov 08 '22 19:11

Basj


1 Answers

Can't you just input a frequency, either shift(1) or shift(-1)

print df.index.shift(1) - df.index

print df.index.shift(-1) - df.index
like image 110
Mad_Hatter Avatar answered Nov 14 '22 22:11

Mad_Hatter