Joe
Joe

Reputation: 468

Compute percent change with vaex dataframe

How can I compute the percent change of a column in a vaex dataframe when using the shift function?

import numpy as np
import vaex
df = vaex.from_arrays(x=np.arange(1,4))
df.x / df.shift(periods=1) - 1

results in an error:

  File "<unknown>", line 3
    1  1
       ^
SyntaxError: invalid syntax

Upvotes: 0

Views: 37

Answers (1)

DougR
DougR

Reputation: 3479

You need to make a copy of the x column and then shift it.

import numpy as np
import vaex
df = vaex.from_arrays(x=np.arange(1,4))
df['x_shifted'] = df.x
df = df.shift(periods=1,column='x_shifted')
df.x/df.x_shifted-1

enter image description here

Upvotes: 1

Related Questions