Transforming financial data from postgres to pandas dataframe for use with Zipline
I suspect you are trying to set the date as the index too early. My suggestion would be to first set_index
as date and company name, then you can unstack
the company name and resample
.
Something like this:
In [11]: df1Out[11]: ticker_symbol monthly_return date0 AAPL 0.112 1992-02-28 00:00:001 GS 0.130 1981-11-30 00:00:002 GS -0.230 1981-12-22 00:00:00df2 = df2.set_index(['date','ticker_symbol'])df3 = df2.unstack(level=1)df4 = df.resample('M')In [14]: df2Out[14]: monthly_returndate ticker_symbol 1992-02-28 AAPL 0.1121981-11-30 GS 0.1301981-12-22 GS -0.230In [15]: df3Out[15]: monthly_return ticker_symbol AAPL GSdate 1981-11-30 NaN 0.131981-12-22 NaN -0.231992-02-28 0.112 NaNIn [16]: df4Out[16]: <class 'pandas.core.frame.DataFrame'>DatetimeIndex: 124 entries, 1981-11-30 00:00:00 to 1992-02-29 00:00:00Freq: MData columns:(monthly_return, AAPL) 1 non-null values(monthly_return, GS) 2 non-null valuesdtypes: float64(2)