Skip to content Skip to sidebar Skip to footer

Rolling Difference In Pandas

Does anyone know an efficient function/method such as pandas.rolling_mean, that would calculate the rolling difference of an array This is my closest solution: roll_diff = pd.Serie

Solution 1:

What about:

importpandasx= pandas.DataFrame({
    'x_1': [0, 1, 2, 3, 0, 1, 2, 500, ],},
    index=[0, 1, 2, 3, 4, 5, 6, 7])

x['x_1'].rolling(window=2).apply(lambda x: x.iloc[1] - x.iloc[0])

in general you can replace the lambda function with your own function. Note that in this case the first item will be NaN.

Update

Defining the following:

n_steps = 2defmy_fun(x):
    return x.iloc[-1] - x.iloc[0]

x['x_1'].rolling(window=n_steps).apply(my_fun)

you can compute the differences between values at n_steps.

Solution 2:

You can do the same thing as in https://stackoverflow.com/a/48345749/1011724 if you work directly on the underlying numpy array:

import numpy as npdiff_kernel= np.array([1,-1])
np.convolve(rs,diff_kernel ,'same')

where rs is your pandas series

Solution 3:

If you got KeyError: 0, try with iloc:

importpandasx= pandas.DataFrame({
    'x_1': [0, 1, 2, 3, 0, 1, 2, 500, ],},
    index=[0, 1, 2, 3, 4, 5, 6, 7])

x['x_1'].rolling(window=2).apply(lambda x: x.iloc[1] - x.iloc[0])

Solution 4:

This should work:

import numpy as np

x = np.array([1, 3, 6, 1, -5, 6, 4, 1, 6])

defrunning_diff(arr, N):
    return np.array([arr[i] - arr[i-N] for i inrange(N, len(arr))])

running_diff(x, 4)  # array([-6,  3, -2,  0, 11])

For a given pd.Series, you will have to define what you want for the first few items. The below example just returns the initial series values.

s_roll_diff = np.hstack((s.values[:4], running_diff(s.values, 4)))

This works because you can assign a np.array directly to a pd.DataFrame, e.g. for a column s, df.s_roll_diff = np.hstack((df.s.values[:4], running_diff(df.s.values, 4)))

Solution 5:

Applying numpy.diff:

import pandas as pd
import numpy as np

x = pd.DataFrame({
    'x_1': [0, 1, 2, 3, 0, 1, 2, 500, ],}
)

print(x)

>>>   x_1
001122334051627500print(x['x_1'].rolling(window=2).apply(np.diff))

>>>0      NaN
11.021.031.04     -3.051.061.07498.0
Name: x_1, dtype: float64

Post a Comment for "Rolling Difference In Pandas"