Pandas: BUG: groupby(.., as_index=False) with a TimeGrouper

Created on 8 Aug 2017  ·  6Comments  ·  Source: pandas-dev/pandas

Hello,

I am doing some basic aggregation and boom, some weird bug occurs.
Here is a reprex:

import pandas as pd
import numpy as np


idx2=[pd.to_datetime('2016-08-31 22:08:12.000') , 
     pd.to_datetime('2016-08-31 22:09:12.200'),
     pd.to_datetime('2016-08-31 22:20:12.400')]

test=pd.DataFrame({'quant':[1.0,1.0,3.0], 
                   'quant2':[1.0,1.0,3.0],
                   'time2':[pd.to_datetime('2016-08-31 22:08:12.000') , 
                             pd.to_datetime('2016-08-31 22:09:12.200'),
                             pd.to_datetime('2016-08-31 22:20:12.400')]}, 
                    index=idx2)
test.reset_index(inplace = True)

test
Out[22]: 
                    index  quant  quant2                   time2
0 2016-08-31 22:08:12.000    1.0     1.0 2016-08-31 22:08:12.000
1 2016-08-31 22:09:12.200    1.0     1.0 2016-08-31 22:09:12.200
2 2016-08-31 22:20:12.400    3.0     3.0 2016-08-31 22:20:12.400

df= test.groupby(pd.Grouper(key='time2', freq='1T', closed = 'left', label = 'left'),as_index = False).agg(
                     {'quant' : 'sum',
                      'quant2' : 'sum'})

gives

  File "<ipython-input-20-c09863316397>", line 19, in <module>
    'quant2' : 'sum'})

  File "C:\\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 4036, in aggregate
    return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs)

  File "C:\\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 3491, in aggregate
    self._insert_inaxis_grouper_inplace(result)

  File "C:\\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 4090, in _insert_inaxis_grouper_inplace
    self.grouper.get_group_levels(),

  File "C:\\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 1911, in get_group_levels
    if not self.compressed and len(self.groupings) == 1:

AttributeError: 'BinGrouper' object has no attribute 'compressed'

Is that expected? Why would the as_index = False generate this error?

Bug Groupby Resample

Most helpful comment

@randomgambit : Thanks! Do you mind moving that code into your initial issue report? That will make it easier for us to read, even at just a glance.

All 6 comments

@randomgambit : Thanks for the report! Unfortunately, we can't replicate this because you didn't specify what df was. Could you provide that in your sample code?

@randomgambit : Thanks! Do you mind moving that code into your initial issue report? That will make it easier for us to read, even at just a glance.

@gfyoung any ideas?

cc @jreback

as_index is not supported when using a TimeGrouper.

note that your example is equiv of

test.resample('1T', on='time2').sum()

I guess this should work.

it's been over 3 years since someone called dibs on this, but if you want a quick workaround, I was able to project the index col to its own column with a .reset_index() after the aggregation:

df.groupby(pd.Grouper(key='date_col', freq='1d')).count().reset_index()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

matthiasroder picture matthiasroder  ·  3Comments

venuktan picture venuktan  ·  3Comments

andreas-thomik picture andreas-thomik  ·  3Comments

scls19fr picture scls19fr  ·  3Comments

ebran picture ebran  ·  3Comments