0

For 1D I can use:

a=np.array([1,2,3,4])
b=pandas.Series(a).rolling(window=3,center=True).mean()

But the problem is, if I have array a, in 3D then using this method gives error

Exception: Data must be 1-dimensional

The code which I used is:

t[:,:,0]=(pd.Series(imgg[:,:,0:4]).rolling(window=[1,1,3],center=True).mean())

Here imgg is 3D numpy array.

What else I tried:

I also tried the old function rolling_mean i.e. pd.rolling_mean(a,4,center=True), but it is also not working, it gives error:

AssertionError: cannot support ndim > 2 for ndarray compat
Prvt_Yadav
  • 324
  • 1
  • 6
  • 20

1 Answers1

1

Okay, hopefully this is what you need.

I think you can try to split up the arrays first, instead of trying to work on a 3-dimensional array - since we know that it works on 1D.

import pandas as pd

imgg = [(1,2,1),(2,3,3),(4,1,2),(5,3,2),(6,2,1),(2,3,4),(5,6,2)]

>>>imgg
   0  1  2
0  1  2  1
1  2  3  3
2  4  1  2
3  5  3  2
4  6  2  1
5  2  3  4
6  5  6  2

x = []
y = []
d = []

# Split into components
for img in imgg:
    x.append(img[0])
    y.append(img[1])
    d.append(img[2])

# Compute rolling mean
dm = pd.Series(d).rolling(window=3,center=True).mean()

# Stitch them back to form your desired dataframe
data = [k for k in zip(x,y,dm)]
df = pd.DataFrame(data)

>>>df
   0  1         2
0  1  2       NaN
1  2  3  2.000000
2  4  1  2.333333
3  5  3  1.666667
4  6  2  2.333333
5  2  3  2.333333
6  5  6       NaN
kerwei
  • 1,822
  • 1
  • 13
  • 22