python - Pandas: Can you access rolling window items -
can access pandas rolling window object.
rs = pd.series(range(10)) rs.rolling(window = 3) #print's rolling [window=3,center=false,axis=0]
can groups?:
[0,1,2] [1,2,3] [2,3,4]
i start off saying reaching in internal impl. if really wanted compute indexers same way pandas.
you need v0.19.0rc1 (just released), can conda install -c pandas pandas=0.19.0rc1
in [41]: rs = pd.series(range(10)) in [42]: rs out[42]: 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 dtype: int64 # reaches internal implementation # first 3 window, second minimum periods # need in [43]: start, end, _, _, _, _ = pandas._window.get_window_indexer(rs.values,3,3,none,use_mock=false) # starting index in [44]: start out[44]: array([0, 0, 0, 1, 2, 3, 4, 5, 6, 7]) # ending index in [45]: end out[45]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # windo size in [3]: end-start out[3]: array([1, 2, 3, 3, 3, 3, 3, 3, 3, 3]) # indexers in [47]: [np.arange(s, e) s, e in zip(start, end)] out[47]: [array([0]), array([0, 1]), array([0, 1, 2]), array([1, 2, 3]), array([2, 3, 4]), array([3, 4, 5]), array([4, 5, 6]), array([5, 6, 7]), array([6, 7, 8]), array([7, 8, 9])]
so sort of trivial in fixed window case, becomes extremely useful in variable window scenario, e.g. in 0.19.0 can specify things 2s
example aggregate by-time.
all of said, getting these indexers not particularly useful. want do results. point of aggregation functions, or .apply
if want generically aggregate.
Comments
Post a Comment