Skip to content

Commit d9fdd00

Browse files
committed
BUG: fix regression in multi-key groupby since 0.5.0, GH #375
1 parent f71665f commit d9fdd00

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pandas/core/groupby.py

+9
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ def _multi_iter(self):
236236
def flatten(gen, level=0, shape_axis=0):
237237
ids = self.groupings[level].ids
238238
for cat, subgen in gen:
239+
if subgen is None:
240+
continue
241+
239242
if isinstance(subgen, tipo):
240243
yield (ids[cat],), subgen
241244
else:
@@ -382,6 +385,10 @@ def _python_agg_general(self, func, *args, **kwargs):
382385
# want to cythonize?
383386
def _doit(reschunk, ctchunk, gen, shape_axis=0):
384387
for i, (_, subgen) in enumerate(gen):
388+
# TODO: fixme
389+
if subgen is None:
390+
continue
391+
385392
if isinstance(subgen, PandasObject):
386393
size = subgen.shape[shape_axis]
387394
ctchunk[i] = size
@@ -1337,7 +1344,9 @@ def slicer(data, slob):
13371344

13381345
# skip empty groups in the cartesian product
13391346
if left == right:
1347+
yield i, None
13401348
continue
1349+
13411350
yield i, slicer(data, slob)
13421351
else:
13431352
# yield subgenerators, yikes

pandas/tests/test_groupby.py

+17
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,23 @@ def f(group):
996996
res = f(group)
997997
assert_frame_equal(res, result.ix[key])
998998

999+
def test_groupby_wrong_multi_labels(self):
1000+
from pandas import read_csv
1001+
from cStringIO import StringIO
1002+
data = """index,foo,bar,baz,spam,data
1003+
0,foo1,bar1,baz1,spam2,20
1004+
1,foo1,bar2,baz1,spam3,30
1005+
2,foo2,bar2,baz1,spam2,40
1006+
3,foo1,bar1,baz2,spam1,50
1007+
4,foo3,bar1,baz2,spam1,60"""
1008+
data = read_csv(StringIO(data), index_col=0)
1009+
1010+
grouped = data.groupby(['foo', 'bar', 'baz', 'spam'])
1011+
1012+
result = grouped.agg(np.mean)
1013+
expected = grouped.mean()
1014+
assert_frame_equal(result, expected)
1015+
9991016
class TestPanelGroupBy(unittest.TestCase):
10001017

10011018
def setUp(self):

0 commit comments

Comments
 (0)