## Sliding windows of NaN padded elements with numpy.lib.stride_tricks.as_strided

Question

consider the array `a`

```
import numpy as np
import pandas as pd
np.random.seed([3,1415])
a = np.random.randint(100, size=10)
print(a)
[11 98 74 90 15 55 13 11 13 26]
```

I'm using `as_strided`

`from numpy.lib.stride_tricks import as_strided`

When I use this to give a rolling window as follows

```
as_strided(a, shape=(len(a), 5), strides=(8, -8))
[[11 0 0 0 0]
[98 11 0 0 0]
[74 98 11 0 0]
[90 74 98 11 0]
[15 90 74 98 11]
[55 15 90 74 98]
[13 55 15 90 74]
[11 13 55 15 90]
[13 11 13 55 15]
[26 13 11 13 55]]
```

This is almost perfect. I want to have `np.nan`

in that top triangle instead of zeros.

I want this

```
[[ 11. nan nan nan nan]
[ 98. 11. nan nan nan]
[ 74. 98. 11. nan nan]
[ 90. 74. 98. 11. nan]
[ 15. 90. 74. 98. 11.]
[ 55. 15. 90. 74. 98.]
[ 13. 55. 15. 90. 74.]
[ 11. 13. 55. 15. 90.]
[ 13. 11. 13. 55. 15.]
[ 26. 13. 11. 13. 55.]]
```

Is there a convenient way to tell `as_strided`

to fill those in with `np.nan`

instead

Show source

## Answers ( 1 )

The trick is to prepend NaNs and then stride it. There could be two ways to stride - forward and backward by using appropriate strides. The way the desired output is set, we need to stride backwards along each row. An alternative method would be to stride forward, get the 2D output and finally flip the columns, though it would be a bit slower. So, using the forward method, we would have as usual a positive stride along each row and with backward striding one a negative stride.

Thus, the two approaches with

`strides`

would be -Sample run -

Runtime test -