简体   繁体   中英

Pandas rolling window over irregular series with float index

I have got a time series with a float index representing minutes from start of an experiment . The observations are not perfectly equally spaced. I am doing a rolling mean . Here some example data:

S = pd.Series([0,3,2,6,4,7,7,9,11,13,12,12,11,9,6,7,3,5,4], 
              index=[0.01,0.13,0.2,0.29,0.4,0.5,0.59,0.68,0.79,0.9,1.0,1.1,1.19,1.29,1.4,1.5,1.6,1.71,1.8])
Sr = S.rolling(3, win_type='triang', center=True).mean()

In my real data the window spans several hundred data points. Thus, i would like it to always span the same time (in index units) , instead of a fixed number of observations. I found that this is possible on datetime indexes, however I need the index to be float for further calculation. Is there any way of doing this without having to convert the index to datetime and back again?

Pseudo-function:

Sr = S.rolling(0.3, win_type='triang', center=True, *on=index*).mean()

Expected output for this example:

for each index i: mean over window from i-0.15 to i+0.15 (with triangular weighting according to distance from i)

I do not think it can be done with the rolling method.

Out of interest, it can be done manually as follows:

from scipy.signal.windows import triang
import numpy as np
import pandas as pd

def triangular(a):
    n = a.size
    b = triang(n) / (n - 1)
    return b @ a

S = pd.Series([0,3,2,6,4,7,7,9,11,13,12,12,11,9,6,7,3,5,4],
              index=[0.01,0.13,0.2,0.29,0.4,0.5,0.59,0.68,0.79,0.9,1.0,1.1,1.19,1.29,1.4,1.5,1.6,1.71,1.8])

df = pd.DataFrame({'S': S})
df['neighbours'] = df.index.to_series().apply(lambda x: [df.loc[index][0] for index in df.index if x - 0.15 < index <= x + 0.15])
df['rolling_mean'] = df.neighbours.apply(lambda x: triangular(np.array(x)))
df.drop('neighbours', axis=1, inplace=True)

print(df)

Output:

       S  rolling_mean
0.01   0          1.50
0.13   3          2.00
0.20   2          3.25
0.29   6          4.50
0.40   4          5.25
0.50   7          6.25
0.59   7          7.50
0.68   9          9.00
0.79  11         11.00
0.90  13         12.25
1.00  12         12.25
1.10  12         11.75
1.19  11         10.75
1.29   9          8.75
1.40   6          7.00
1.50   7          5.75
1.60   3          4.50
1.71   5          4.25
1.80   4          4.50

I doubt, however, that this is simpler than converting the float index into datetime and then back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM