get_group sometimes throws an exception when using an index of tuples with different lengths · Issue #8121 · pandas-dev/pandas (original) (raw)
Here is a simple test case that exposes the problem:
df = pd.DataFrame(pd.Series([(1,), (1,2), (1,), (1, 2)]), columns = ['ids'])
gb = df.groupby('ids')
for i in gb.size().index :
print i
gb.get_group(i)
The issues is that in _get_index of GroupBy, these lines assume that if there is a tuple in the index, then the index is a multi-index, which in the above test case isn't true. Maybe there is some other way to detect that values are from a multi-index, or should pandas explicitly not support tuples in this situation (in an index of a groupby)
pandas/core/groupby.py:
sample = next(iter(self.indices))
if isinstance(sample, tuple):
if not isinstance(name, tuple):
raise ValueError("must supply a tuple to get_group with multiple grouping keys")
if not len(name) == len(sample):
raise ValueError("must supply a a same-length tuple to get_group with multiple grouping keys")