Skip to content Skip to sidebar Skip to footer

Looking At Previous Time Series

I Have a dataset as shown below. The idea is looking at every previous 15minutes not the frequency which we use in grouper function. I want to see the number of positive changes in

Solution 1:

You can use:

#create DatetimeIndex if necessary
#df = df.set_index('timestamp')

#get unique values with counts by comparing and sum of True
cols = df['Direction'].dropna().unique()
for c in cols:
    df[c] = df['Direction'].eq(c).rolling('15min').sum()

#if necessary set first 14 minutes to NaNs
df.loc[:df.index[0] + pd.Timedelta(14 * 60, unit='s'), cols] = np.nan

print (df)
                     row Direction   Positive  Neg  Nut  Pos
timestamp                                                   
2019-01-20 12:15:00    1       NaN        NaN  NaN  NaN  NaN
2019-01-20 12:17:00    2       Nut        NaN  NaN  NaN  NaN
2019-01-20 12:17:00    3       Neg        NaN  NaN  NaN  NaN
2019-01-20 12:18:00    4       Neg        NaN  NaN  NaN  NaN
2019-01-20 12:19:00    5       Pos        NaN  NaN  NaN  NaN
2019-01-20 12:20:00    6       Neg        NaN  NaN  NaN  NaN
2019-01-20 12:21:00    7       Neg        NaN  NaN  NaN  NaN
2019-01-20 12:22:00    8       Pos        NaN  NaN  NaN  NaN
2019-01-20 12:23:00    9       Neg        NaN  NaN  NaN  NaN
2019-01-20 12:24:00   10       Pos        NaN  NaN  NaN  NaN
2019-01-20 12:25:00   11       Neg        NaN  NaN  NaN  NaN
2019-01-20 12:26:00   12       Neg        NaN  NaN  NaN  NaN
2019-01-20 12:27:00   13       Neg        NaN  NaN  NaN  NaN
2019-01-20 12:29:00   14       Neg        NaN  NaN  NaN  NaN
2019-01-20 12:29:00   15       Nut        NaN  NaN  NaN  NaN
2019-01-20 12:30:00   16       Pos  4(o2:o16)  9.0  2.0  4.0
2019-01-20 12:31:00   17       Nut  4(o3:o17)  9.0  3.0  4.0
2019-01-20 12:32:00   18       Pos  5(o4:o18)  8.0  2.0  5.0

Post a Comment for "Looking At Previous Time Series"