Skip to content

Commit b3d1a05

Browse files
benjschillerjreback
authored andcommitted
PR 9090 Fixes .nth() with groupby multiple columns
1 parent 28fc02a commit b3d1a05

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

doc/source/whatsnew/v0.16.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,4 @@ Bug Fixes
140140
- DataFrame now properly supports simultaneous ``copy`` and ``dtype`` arguments in constructor (:issue:`9099`)
141141
- Bug in read_csv when using skiprows on a file with CR line endings with the c engine. (:issue:`9079`)
142142
- isnull now detects NaT in PeriodIndex (:issue:`9129`)
143+
- Bug in groupby ``.nth()`` with a multiple column groupby (:issue:`8979`)

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ def nth(self, n, dropna=None):
859859
# this is a pass-thru
860860
pass
861861
elif all([ n in ax for n in names ]):
862-
result.index = Index(self.obj[names][is_nth].values.ravel()).set_names(names)
862+
result.index = MultiIndex.from_arrays([self.obj[name][is_nth] for name in names]).set_names(names)
863863
elif self._group_selection is not None:
864864
result.index = self.obj._get_axis(self.axis)[is_nth]
865865

pandas/tests/test_groupby.py

+29
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,35 @@ def test_nth(self):
337337
expected = DataFrame(1, columns=['a', 'b'], index=expected_dates)
338338
assert_frame_equal(result, expected)
339339

340+
def test_nth_multi_index(self):
341+
# PR 9090, related to issue 8979
342+
# test nth on MultiIndex, should match .first()
343+
grouped = self.three_group.groupby(['A', 'B'])
344+
result = grouped.nth(0)
345+
expected = grouped.first()
346+
assert_frame_equal(result, expected)
347+
348+
349+
def test_nth_multi_index_as_expected(self):
350+
# PR 9090, related to issue 8979
351+
# test nth on MultiIndex
352+
three_group = DataFrame({'A': ['foo', 'foo', 'foo', 'foo',
353+
'bar', 'bar', 'bar', 'bar',
354+
'foo', 'foo', 'foo'],
355+
'B': ['one', 'one', 'one', 'two',
356+
'one', 'one', 'one', 'two',
357+
'two', 'two', 'one'],
358+
'C': ['dull', 'dull', 'shiny', 'dull',
359+
'dull', 'shiny', 'shiny', 'dull',
360+
'shiny', 'shiny', 'shiny']})
361+
grouped = three_group.groupby(['A', 'B'])
362+
result = grouped.nth(0)
363+
expected = DataFrame({'C': ['dull', 'dull', 'dull', 'dull']},
364+
index=MultiIndex.from_arrays([['bar', 'bar', 'foo', 'foo'], ['one', 'two', 'one', 'two']],
365+
names=['A', 'B']))
366+
assert_frame_equal(result, expected)
367+
368+
340369
def test_grouper_index_types(self):
341370
# related GH5375
342371
# groupby misbehaving when using a Floatlike index

0 commit comments

Comments
 (0)