简体   繁体   中英

Get if a rolling window is increasing or decreasing

I have a panda's DataFrame and I would like to calculate for each interval of a moving window along one of its columns whether the data inside that interval is increasing or decreasing. For increasing/ decreasing I mean to calculate the sign of the last element minus the first element.

For now I have this solution:

sign = data['col'].rolling('5d').apply(lambda x: np.sign(x[-1] - x[0]))

The problem of this implementation is that it's extremely slow for long Series. Do you have a solution that uses builtin, optimised functions?

If your 'col' values are uniformly sampled you can apply something similar to below.

col = pandas.Series([1,-1,0,3,5,21,7,4,67,4,3,6,8,5,4])
sign = np.sign(col.values[0:-5] - col.values[5:])

Essentially you take the first N-window_len and subtract the last N-window_len (here I assume you have a data point for each day). vector operation would be significantly faster than using rolling

You are only getting the first value and last value different by using a memory cost rolling function which will reduce the speed of whole process , Here I am using merge_asof

s=df[['COL']].shift(4,freq='D')
s=pd.merge_asof(df,s,left_index=True,right_index=True,tolerance=pd.Timedelta('5d'),direction='forward')
np.sign(s['COL_x']-s['COL_y'])

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