Pandas: BUG:使用TimeGrouper的groupby(..,as_index = False)

创建于 2017-08-08  ·  6评论  ·  资料来源: pandas-dev/pandas

你好,

我正在做一些基本的汇总和繁荣,出现一些奇怪的错误。
这里是一个代表:

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'})

  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'

那是预期的吗? 为什么as_index = False产生此错误?

Bug Groupby Resample

最有用的评论

@randomgambit :谢谢! 您介意将该代码移入初始问题报告吗? 即使一目了然,这也使我们更容易阅读。

所有6条评论

@randomgambit :感谢您的举报! 不幸的是,我们无法复制此内容,因为您未指定df是什么。 您可以在示例代码中提供它吗?

@randomgambit :谢谢! 您介意将该代码移入初始问题报告吗? 即使一目了然,这也使我们更容易阅读。

@gfyoung有什么想法吗?

cc @jreback

使用TimeGrouper时不支持as_index

请注意,您的示例等效于

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

我想这应该工作。

自从有人叫dibs到现在已有3年了,但是如果您想要一种快速的解决方法,我可以在聚合后使用.reset_index()将索引col投影到它自己的列上:

df.groupby(pd.Grouper(key='date_col', freq='1d')).count().reset_index()
此页面是否有帮助?
0 / 5 - 0 等级