question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
cpdeancommented, 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()
3reactions
gfyoungcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas grouper issue with key that is an index - Stack Overflow
I think you need: pd.Grouper(level='Time',freq='15Min'). I believe you can add column Response to groupby , reshape by unstack and plot:
Read more >
pandas.core.groupby.DataFrameGroupBy.resample
Provide resampling when using a TimeGrouper. Given a grouper, the function resamples it according to a string “string” -> “frequency”. See the frequency...
Read more >
pandas - BUG : TimeGrouper가있는 groupby (.., as_index = False ...
기본 집계 및 붐을 수행 중이며 이상한 버그가 발생합니다. 다음은 reprex입니다. import pandas as pd import numpy ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found