Simd
Simd

Reputation: 21223

How to make a rolling gradient column for a dataframe

I have a time series like:

            United Kingdom
Time (CET)                
2020-02-15         1739771
2020-02-16         1649750
2020-02-17         1887394
2020-02-18         1922006
2020-02-19         1957194
2020-02-20         1878259
2020-02-21         1890076
2020-02-22         1677928
2020-02-23         1688218
2020-02-24         1948947
[...]

I would like to make a new column which is the gradient of a rolling window of length 5. So the first entry will be the gradient of a straight line fitted through [1739771, 1649750, 1887394, 1922006, 1957194], the second the gradient of a straight line fitted through [1649750, 1887394, 1922006, 1957194, 1878259] and so on.

The code to fit a straight line and hence return the gradient is:

import numpy as np
Polynomial = np.polynomial.Polynomial
def fitcurve(sequence):
    poly = Polynomial.fit(range(len(sequence)), sequence, 1)
    a = poly.convert().coef
    return a[0], a[1]  #  a[1] is the gradient we want.

The first 4 entries in the new column will have to be NaNs which is ok.

How can I make this new column?

Upvotes: 0

Views: 333

Answers (1)

Daniel Geffen
Daniel Geffen

Reputation: 1862

You should use the rolling function. Something like this should work:

df["UK Rolling"] = df["United Kingdom"].rolling(5).apply(lambda x: fitcurve(x)[1])

Upvotes: 2

Related Questions