2

Using pandas, I have a DataFrame consisting of MultiIndex Series where level 0 is a DateTimeindex (from date_range). Now, when I resample this dataframe (I go from monthly data points to quarterly for example, so I'm downscaling), I seem to lose level 1 from the MultiIndex Series.

I suspect it happens because of the sum() I'm applying to the DatetimeIndexResampler, but how can I prevent this from happening?

To give you an idea of the DataFrame:

                                A             B             C  
2017-03-31 a             EUR 0.00      EUR 0.00      EUR 0.00  
           b             EUR 0.00      EUR 0.00      EUR 0.00  
           c             EUR 0.00    EUR 895.00    EUR 600.00  
           d             EUR 0.00    EUR 468.00    EUR 771.43  
           e             EUR 0.00  EUR 4,120.00  EUR 6,048.57  
2017-04-30 a             EUR 0.00      EUR 0.00      EUR 0.00  
           b             EUR 0.00      EUR 0.00      EUR 0.00  
           c             EUR 0.00    EUR 895.00    EUR 600.00  
           d             EUR 0.00    EUR 468.00    EUR 800.00  
           e             EUR 0.00  EUR 4,120.00  EUR 6,290.00  
2017-05-31 a             EUR 0.00      EUR 0.00      EUR 0.00  
           b         EUR 4,333.33      EUR 0.00      EUR 0.00  
           c           EUR 995.00    EUR 895.00    EUR 600.00  
           d           EUR 790.00    EUR 468.00    EUR 800.00  
           e         EUR 3,650.00  EUR 4,120.00  EUR 6,290.00

It is stored in the variable values.

Then:

values.resample('QS', level=0).sum()

returns

                         A              B              C
2017-01-01        EUR 0.00   EUR 5,483.00   EUR 7,420.00  
2017-04-01    EUR 9,768.33  EUR 10,966.00  EUR 15,380.00  

So, where did my MultiIndex go? I am hoping to be able to sum over 'a', 'b', 'c', 'd' and 'e' separately when resampling.

Bart Van Loon
  • 1,430
  • 8
  • 18
  • Ah, damnit. I found those answers as well, but didn't find the applicable to my situation. But now I found that `values.groupby([pd.Grouper(freq='QS', level=0), level_values(1)]).sum()` does exactly what I want. Thanks! – Bart Van Loon Sep 27 '17 at 16:04

0 Answers0