python - Extract monthly categorical (dummy) variables in pandas from a time series -
so have dataframe (df) dated data on monthly time series (end of month). looks this:
date data 2010-01-31 625000 2010-02-28 750000 ... 2014-10-31 450000 2014-11-30 475000
i check on seasonal monthly effects.
this simple do, how can go extracting month date create categorical dummy variables use in regression?
i want this:
date 01 02 03 04 05 06 07 08 09 10 11 2010-01-31 1 0 0 0 0 0 0 0 0 0 0 2010-02-28 0 1 0 0 0 0 0 0 0 0 0 ... 2014-10-31 0 1 0 0 0 0 0 0 0 1 0 2014-11-30 0 1 0 0 0 0 0 0 0 0 1
i tried using pd.dataframe(df.index.month, index=df.index)... gives me month each date. believe need use pandas.core.reshape.get_dummies variables in 0/1 matrix format. can show me how? thanks.
this how got april:
import pandas pd import numpy np dates = pd.date_range('20130101', periods=4, freq='ms') df = pd.dataframe(np.random.randn(4), index=dates, columns=['data']) df.ix[dates.month==4]
the idea make dates index , boolean index selection on dataframe.
>>> df data 2013-01-01 0.141205 2013-02-01 0.115361 2013-03-01 -0.309521 2013-04-01 -0.236317 >>> df.ix[dates.month==4] data 2013-04-01 -0.236317
Comments
Post a Comment