1

I've got several different futures price time series combined into one data frame using concat. For example, soybeans and corn futures contracts for a number of different years.

I would like to compare the prices of the varying contracts graphically on the same time basis, in this case Nov 1 through July 1, disregarding the year. To do that, I used reset_index() to combine them together.

Only problem is when I graph them, I'd like to have an x-axis that is a bit more intuitive than just a 0 to 160 numerical index interval. Ideally, I would like to have the labels Nov 1 through Jul 1 monthly and omit the year.

This of course is fairly easy to do in excel, but I’d like to be able to do it in Python as well.

dfc14 = quandl.get('CME/CZ2014.6', start_date='2013-11-01', end_date='2014-07-\ 01', authtoken=api_key)

dfs14 = quandl.get('CME/SX2014.6', start_date='2013-11-01', end_date='2014-07-01', authtoken=api_key)
dfr14 = dfs14.div(dfc14)
dfr14 = dfr14.rename(columns={'Settle':'2014'})
dfr14 = dfr14.reset_index()
# ...
com_df = pd.concat([dfr17, dfr16, dfr14],)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
judabomber
  • 21
  • 2

1 Answers1

0

The way I did this was to get a mmdd column and use that as the index -- for the x-axis and x-axis labels.

I did it for two of the three DataFrames.

dfr14['mmdd'] = (dfr14['Date'].dt.month.astype(str).str.zfill(2) + 
                 dfr14['Date'].dt.day.astype(str).str.zfill(2))
dfr15['mmdd'] = (dfr15['Date'].dt.month.astype(str).str.zfill(2) + 
                 dfr15['Date'].dt.day.astype(str).str.zfill(2))

dfr15 = dfr15.set_index('mmdd')
dfr14 = dfr14.set_index('mmdd')

ax = dfr15.plot()
dfr14.plot(ax=ax)
plt.show()

Result of the two:

enter image description here

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223