Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
randomgambit opened this issue Aug 8, 2017 · 9 comments · Fixed by #53173
Closed

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

randomgambit opened this issue Aug 8, 2017 · 9 comments · Fixed by #53173
Assignees
Labels
Bug good first issue Groupby Needs Tests Unit test(s) needed to prevent regressions Resample resample method

Comments

@randomgambit
Copy link

randomgambit commented Aug 8, 2017

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?

@gfyoung gfyoung added the Groupby label Aug 8, 2017
@gfyoung
Copy link
Member

gfyoung commented Aug 8, 2017

@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?

@gfyoung
Copy link
Member

gfyoung commented Aug 8, 2017

@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.

@randomgambit
Copy link
Author

@gfyoung any ideas?

@gfyoung
Copy link
Member

gfyoung commented Aug 9, 2017

cc @jreback

@jreback
Copy link
Contributor

jreback commented Aug 9, 2017

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.

@jreback jreback added this to the Next Major Release milestone Aug 9, 2017
@jreback jreback changed the title weird error : 'BinGrouper' object has no attribute 'compressed' when using Grouper + as_index BUG: groupby(.., as_index=False) with a TimeGrouper Aug 9, 2017
@cpdean
Copy link

cpdean commented Aug 24, 2020

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()

@Mamadou442
Copy link

Mamadou442 commented Jul 15, 2022

hi, is this issue still active, if so i would like to contribute and want to know how

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@rhshadrach
Copy link
Member

This appears to be working now on main, needs tests.

@rhshadrach rhshadrach added good first issue Needs Tests Unit test(s) needed to prevent regressions labels Dec 23, 2022
@mahaoyu
Copy link

mahaoyu commented Mar 18, 2023

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug good first issue Groupby Needs Tests Unit test(s) needed to prevent regressions Resample resample method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants