python - How to use days as window for pandas rolling_apply function -


i have pandas dataframe irregularly spaced dates. there way use 7days moving window calculate median absolute deviation, median etc..? feel somehow use pandas.rolling_apply not take irregularly spaced dates window parameter. found similar post https://stackoverflow.com/a/30244019/3128336 , trying create custom function cannot still figure out.. can please help?

import pandas pd datetime import datetime  person = ['a','b','c','b','a','c','a','b','c','a',] ts = [     datetime(2000, 1, 1),     datetime(2000, 1, 1),     datetime(2000, 1, 10),     datetime(2000, 1, 20),     datetime(2000, 1, 25),     datetime(2000, 1, 30),     datetime(2000, 2, 8),     datetime(2000, 2, 12),     datetime(2000, 2, 17),     datetime(2000, 2, 20), ] score = [9,2,1,3,8,4,2,3,1,9] df = pd.dataframe({'ts': ts, 'person': person, 'score': score}) 

df looks this

    person  score   ts 0         9       2000-01-01 1   b       2       2000-01-01 2   c       1       2000-01-10 3   b       3       2000-01-20 4         8       2000-01-25 5   c       4       2000-01-30 6         2       2000-02-08 7   b       3       2000-02-12 8   c       1       2000-02-17 9         9       2000-02-20 

you can use time delta select rows within window , use apply run through each row , aggregate:

>>> datetime import timedelta >>> delta = timedelta(days=7) >>> df_score_mean = df.apply(lambda x: np.mean(df['score'][df['ts'] <= x['ts'] + delta]), axis=1) 0    5.500000 1    5.500000 2    4.000000 3    4.600000 4    4.500000 5    4.500000 6    4.555556 7    4.200000 8    4.200000 9    4.200000 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -