Skip to content

Commit c3bebac

Browse files
committed
Merge pull request #7094 from jreback/groupby_nth_repr
REGR: Regression in groupby.nth() for out-of-bounds indexers (GH6621)
2 parents ebc4fed + c8e65ae commit c3bebac

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ Bug Fixes
490490
- Bug in ``MultiIndex.from_arrays`` created from ``DatetimeIndex`` doesn't preserve ``freq`` and ``tz`` (:issue:`7090`)
491491
- Bug in ``unstack`` raises ``ValueError`` when ``MultiIndex`` contains ``PeriodIndex`` (:issue:`4342`)
492492
- Bug in ``boxplot`` and ``hist`` draws unnecessary axes (:issue:`6769`)
493+
- Regression in ``groupby.nth()`` for out-of-bounds indexers (:issue:`6621`)
493494

494495
pandas 0.13.1
495496
-------------

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ def _reindex(keys, level=None):
891891
return self.obj.take(indexer, axis=axis)
892892

893893
# this is not the most robust, but...
894-
if (isinstance(labels, MultiIndex) and
894+
if (isinstance(labels, MultiIndex) and len(keyarr) and
895895
not isinstance(keyarr[0], tuple)):
896896
level = 0
897897
else:

pandas/tests/test_groupby.py

+14
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,20 @@ def test_nth(self):
241241
assert_frame_equal(g.nth(7, dropna='any'), exp.iloc[[1, 2]])
242242
assert_frame_equal(g.nth(2, dropna='any'), exp.iloc[[1, 2]])
243243

244+
# out of bounds, regression from 0.13.1
245+
# GH 6621
246+
df = DataFrame({'color': {0: 'green', 1: 'green', 2: 'red', 3: 'red', 4: 'red'},
247+
'food': {0: 'ham', 1: 'eggs', 2: 'eggs', 3: 'ham', 4: 'pork'},
248+
'two': {0: 1.5456590000000001, 1: -0.070345000000000005, 2: -2.4004539999999999, 3: 0.46206000000000003, 4: 0.52350799999999997},
249+
'one': {0: 0.56573799999999996, 1: -0.9742360000000001, 2: 1.033801, 3: -0.78543499999999999, 4: 0.70422799999999997}}).set_index(['color', 'food'])
250+
251+
result = df.groupby(level=0).nth(2)
252+
expected = df.iloc[[-1]]
253+
assert_frame_equal(result,expected)
254+
255+
result = df.groupby(level=0).nth(3)
256+
expected = df.loc[[]]
257+
assert_frame_equal(result,expected)
244258

245259
def test_grouper_index_types(self):
246260
# related GH5375

pandas/tests/test_indexing.py

+3
Original file line numberDiff line numberDiff line change
@@ -3253,6 +3253,7 @@ def test_iloc_empty_list_indexer_is_ok(self):
32533253
df = mkdf(5, 2)
32543254
assert_frame_equal(df.iloc[:,[]], df.iloc[:, :0]) # vertical empty
32553255
assert_frame_equal(df.iloc[[],:], df.iloc[:0, :]) # horizontal empty
3256+
assert_frame_equal(df.iloc[[]], df.iloc[:0, :]) # horizontal empty
32563257

32573258
# FIXME: fix loc & xs
32583259
def test_loc_empty_list_indexer_is_ok(self):
@@ -3261,13 +3262,15 @@ def test_loc_empty_list_indexer_is_ok(self):
32613262
df = mkdf(5, 2)
32623263
assert_frame_equal(df.loc[:,[]], df.iloc[:, :0]) # vertical empty
32633264
assert_frame_equal(df.loc[[],:], df.iloc[:0, :]) # horizontal empty
3265+
assert_frame_equal(df.loc[[]], df.iloc[:0, :]) # horizontal empty
32643266

32653267
def test_ix_empty_list_indexer_is_ok(self):
32663268
raise nose.SkipTest('ix discards columns names')
32673269
from pandas.util.testing import makeCustomDataframe as mkdf
32683270
df = mkdf(5, 2)
32693271
assert_frame_equal(df.ix[:,[]], df.iloc[:, :0]) # vertical empty
32703272
assert_frame_equal(df.ix[[],:], df.iloc[:0, :]) # horizontal empty
3273+
assert_frame_equal(df.ix[[]], df.iloc[:0, :]) # horizontal empty
32713274

32723275
def test_deprecate_float_indexers(self):
32733276

0 commit comments

Comments
 (0)