Search code examples
python-3.xvaex

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

Solution

  • 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