Repository: pandas-dev/pandas

Issue:
groupby nth for multiindex regression
regression in groupby nth, it raises an index error on MI in master. This is my fault (need to add tests, should also test for axis!=0).

http://stackoverflow.com/questions/22361385/integer-position-based-indexing-of-multi-index-dataframes

reason this raise is as I use obj.loc[[[]] :s



Discussion:
really? I wrote that answer on master 

example?

Ah, think difference is using manual index rather than levels kwarg:

```
df = pd.DataFrame({'color': {0: 'green', 1: 'green', 2: 'red', 3: 'red', 4: 'red'}, 'food': {0: 'ham', 1: 'eggs', 2: 'eggs', 3: 'ham', 4: 'pork'}, 'two': {0: 1.5456590000000001, 1: -0.070345000000000005, 2: -2.4004539999999999, 3: 0.46206000000000003, 4: 0.52350799999999997}, 'one': {0: 0.56573799999999996, 1: -0.9742360000000001, 2: 1.033801, 3: -0.78543499999999999, 4: 0.70422799999999997}}).set_index(['color', 'food'])

In [8]: df.groupby(level=0).nth(2)
Out[8]: 
                 one       two
color food                    
red   pork  0.704228  0.523508

In [9]: df.groupby(level=0).nth(3)
IndexError: index out of bounds
```

It's possible this is a one char change (to use iloc rather than loc).

Would be good to fix on axis behaviour here (clearly there was no coverage before and I don't think it worked).

@hayd you doing for 0.14?

Should be in 0.14, I'll have some more time to do all these at the end of the week / next week. Very behind with all these PRs (should've been doing them incrementally rather than saving them all up)! :s

hahah...ok np

@hayd ping!

@hayd do you have something for this? (or is this covered already?)

is this a regression?

@hayd ?

yeh..this is easy....i'll put up a PR


Resolving patch:
diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py
--- a/pandas/core/indexing.py
+++ b/pandas/core/indexing.py
@@ -891,7 +891,7 @@ def _reindex(keys, level=None):
                 return self.obj.take(indexer, axis=axis)
 
             # this is not the most robust, but...
-            if (isinstance(labels, MultiIndex) and
+            if (isinstance(labels, MultiIndex) and len(keyarr) and
                     not isinstance(keyarr[0], tuple)):
                 level = 0
             else:


Test patch:
diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py
--- a/pandas/tests/test_groupby.py
+++ b/pandas/tests/test_groupby.py
@@ -241,6 +241,20 @@ def test_nth(self):
         assert_frame_equal(g.nth(7, dropna='any'), exp.iloc[[1, 2]])
         assert_frame_equal(g.nth(2, dropna='any'), exp.iloc[[1, 2]])
 
+        # out of bounds, regression from 0.13.1
+        # GH 6621
+        df = DataFrame({'color': {0: 'green', 1: 'green', 2: 'red', 3: 'red', 4: 'red'},
+                        'food': {0: 'ham', 1: 'eggs', 2: 'eggs', 3: 'ham', 4: 'pork'},
+                        'two': {0: 1.5456590000000001, 1: -0.070345000000000005, 2: -2.4004539999999999, 3: 0.46206000000000003, 4: 0.52350799999999997},
+                        'one': {0: 0.56573799999999996, 1: -0.9742360000000001, 2: 1.033801, 3: -0.78543499999999999, 4: 0.70422799999999997}}).set_index(['color', 'food'])
+
+        result = df.groupby(level=0).nth(2)
+        expected = df.iloc[[-1]]
+        assert_frame_equal(result,expected)
+
+        result = df.groupby(level=0).nth(3)
+        expected = df.loc[[]]
+        assert_frame_equal(result,expected)
 
     def test_grouper_index_types(self):
         # related GH5375
diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py
--- a/pandas/tests/test_indexing.py
+++ b/pandas/tests/test_indexing.py
@@ -3253,6 +3253,7 @@ def test_iloc_empty_list_indexer_is_ok(self):
         df = mkdf(5, 2)
         assert_frame_equal(df.iloc[:,[]], df.iloc[:, :0])  # vertical empty
         assert_frame_equal(df.iloc[[],:], df.iloc[:0, :])  # horizontal empty
+        assert_frame_equal(df.iloc[[]], df.iloc[:0, :])  # horizontal empty
 
     # FIXME: fix loc & xs
     def test_loc_empty_list_indexer_is_ok(self):
@@ -3261,6 +3262,7 @@ def test_loc_empty_list_indexer_is_ok(self):
         df = mkdf(5, 2)
         assert_frame_equal(df.loc[:,[]], df.iloc[:, :0])  # vertical empty
         assert_frame_equal(df.loc[[],:], df.iloc[:0, :])  # horizontal empty
+        assert_frame_equal(df.loc[[]], df.iloc[:0, :])  # horizontal empty
 
     def test_ix_empty_list_indexer_is_ok(self):
         raise nose.SkipTest('ix discards columns names')
@@ -3268,6 +3270,7 @@ def test_ix_empty_list_indexer_is_ok(self):
         df = mkdf(5, 2)
         assert_frame_equal(df.ix[:,[]], df.iloc[:, :0])  # vertical empty
         assert_frame_equal(df.ix[[],:], df.iloc[:0, :])  # horizontal empty
+        assert_frame_equal(df.ix[[]], df.iloc[:0, :])  # horizontal empty
 
     def test_deprecate_float_indexers(self):