pandas groupby causing keyerror
pandas
Solution
Because when you do:
abc= abc.set_index('date').to_period('Q')
You've changed 'date' to a index.
Later you try to access 'date' as a column which doesn't exist any more, hence the error. a reset_index operation restores 'date' as a column so it works again.
You can retain 'date' as column while setting it as index by doing:
abc= abc.set_index('date', drop=False).to_period('Q').groupby(['origin',
'date']).agg(sum)
Problem
I'm trying transform monthly time series data to quarterly then aggregate sums per quarter. I get a KeyError: 'date' when I run the following. ``` abc= abc.set_index('date').to_period('Q').groupby(['origin', 'date']).agg(sum) ``` However, when I reset the index as shown below, the code works. Why do I need to reset the index in order to use groupby on the origin and date fields? Is there a way to group without resetting the index? ``` abc= abc.set_index('date').to_period('Q').reset_index().groupby(['origin', 'date']).agg(sum) ```