Pythonic - 5 months ago 42x

Python Question

Given a pandas Series

`a`

`a[i]`

`a[i-window:i-1]`

`a[i]`

The code below does the job through a python for loop, which is slow on serious computational tasks

Does Pandas offer a similar functionality, possibly wrapping some optimised Numpy function?

`import numpy as np`

import pandas

window = 30 # any arbitrary window

a = pandas.Series(np.random.rand(100)) # dummy variable, arbitrary length

counter = pandas.Series(data=np.NaN, index=a.index)

for i in a.index[window:]:

counter[i] = (a[i-window:i-1] < a[i]).sum()

print counter

Answer

You can use `pd.rolling_apply`

```
import numpy as np
import pandas as pd
window = 30
df = pd.DataFrame(np.random.randn(100), columns=['Data'])
counts = pd.rolling_apply(df, window+1, lambda s: (s < s[-1]).sum())
```

Make sure to add one to the window size.

Source (Stackoverflow)

Comments