python - How to get a correct market day in a DataFrame with pandas.DataFrame.resample? -
there issue confused me. set flag ( isopen ) 1 market day, , set flag 0 days market not open. example, although 12/31/1999 friday(business day), it's not trading on market. set flag (isopen) equal 0.
now want last market day of every month. code not running expect.
import pandas pd pd_index=pd.date_range('12/27/1999', periods=9, freq='d') df1 = pd.dataframe({'isopen':[1,1,1,1,0,0,0,1,1]}, index = pd_index) print('\n----df1----') print(df1) df2 = df1[df1['isopen']==1] print('\n----df2----') print(df2) # df2 not contain 1999-12-31 , last day 2000-01-04 df3=df2.resample('m').last() print('\n----df3----') print(df3)
output:
----df1---- isopen 1999-12-27 1 1999-12-28 1 1999-12-29 1 1999-12-30 1 1999-12-31 0 2000-01-01 0 2000-01-02 0 2000-01-03 1 2000-01-04 1 ----df2---- isopen 1999-12-27 1 1999-12-28 1 1999-12-29 1 1999-12-30 1 2000-01-03 1 2000-01-04 1 ----df3---- isopen 1999-12-31 1 2000-01-31 1
i want expect result below.
----df3---- isopen 1999-12-30 1 2000-01-04 1
but df3 has 2 rows not contain in df2, curious why did not expect output code. other procedure can expect result? thanks.
Comments
Post a Comment