Basj
Basj

Reputation: 46483

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.

Upvotes: 5

Views: 299

Answers (1)

Mad_Hatter
Mad_Hatter

Reputation: 47

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

Upvotes: 1

Related Questions