python - sampling pandas dataframe by different frequencies -


i have multi-index series/dataframe id , timestamp key. data structure has daily data various ids. can use resample function @ end of month snapshot of data structure ?

id ts           value  1  2001-01-30   1    2001-01-31   2    2001-02-01   3 2  2001-01-30   3    2001-01-31   2    2001-02-01   4 

i want output

id  ts          value 1   2001-01-31  2 2   2001-01-31  2 

can use resample function call me out? know can create end of month date list , loop through dates , find values. want use full functionality of pandas.

why need resample? set index ts , slice, so:

from cstringio import stringio raw = """id  ts     value 1  2001-01-30   1 1  2001-01-31   2 1  2001-02-01   3 2  2001-01-30   3 2  2001-01-31   2 2  2001-02-01   4""" sio = stringio(raw) df = read_csv(sio, sep=r'\s+', header=0, parse_dates=[1]) df.set_index('ts', inplace=true) 

slice , reset index:

print df['2001-01-31'].reset_index().set_index('id') 

resulting in:

                    ts  value id                            1  2001-01-31 00:00:00      2 2  2001-01-31 00:00:00      2 

if don't care end of month values inferred if don't exist can this:

df.groupby('id', as_index=false).resample('m', how='last') 

which gives

            id  value ts                    2001-01-31   1      2 2001-02-28   1      3 2001-01-31   2      2 2001-02-28   2      4 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -