Repository: pandas-dev/pandas

Issue:
KeyError when indexing both axes with .loc[] with duplicate indices
The last line of the following code snippet does not work, while it seems it should. Am I overlooking something?

```
df = pd.DataFrame(np.random.randn(9,2), index=[1,1,1,2,2,2,3,3,3], columns=['a', 'b'])
df
df.loc[[1, 2]]    # works
df.loc[:,['a', 'b']]    # works
df.loc[[1, 2], ['a', 'b']]    # does not work
```

You don't have this with an index without duplicates.

```
In [1]: df = pd.DataFrame(np.random.randn(9,2), index=[1,1,1,2,2,2,3,3,3], colum
ns=['a', 'b'])

In [2]: df
Out[2]:
          a         b
1  0.714735 -1.125141
1  0.061066 -0.737312
1  0.269988  1.434862
2  0.336829  1.040681
2  0.929521 -1.142978
2  0.272813 -0.370184
3 -0.520348  1.126233
3  0.232261 -0.075287
3 -0.314570 -2.635400

[9 rows x 2 columns]

In [3]: df.loc[[1, 2]]
Out[3]:
          a         b
1  0.714735 -1.125141
1  0.061066 -0.737312
1  0.269988  1.434862
2  0.336829  1.040681
2  0.929521 -1.142978
2  0.272813 -0.370184

[6 rows x 2 columns]

In [4]: df.loc[:,['a', 'b']]
Out[4]:
          a         b
1  0.714735 -1.125141
1  0.061066 -0.737312
1  0.269988  1.434862
2  0.336829  1.040681
2  0.929521 -1.142978
2  0.272813 -0.370184
3 -0.520348  1.126233
3  0.232261 -0.075287
3 -0.314570 -2.635400

[9 rows x 2 columns]

In [5]: df.loc[[1, 2], ['a', 'b']]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-617013ef905a> in <module>()
----> 1 df.loc[[1, 2], ['a', 'b']]

c:\users\vdbosscj\scipy\pandas-joris\pandas\core\indexing.pyc in __getitem__(sel
f, key)
   1152     def __getitem__(self, key):
   1153         if type(key) is tuple:
-> 1154             return self._getitem_tuple(key)
   1155         else:
   1156             return self._getitem_axis(key, axis=0)

c:\users\vdbosscj\scipy\pandas-joris\pandas\core\indexing.pyc in _getitem_tuple(
self, tup)
    680         # ugly hack for GH #836
    681         if self._multi_take_opportunity(tup):
--> 682             return self._multi_take(tup)
    683
    684         # no shortcut needed

c:\users\vdbosscj\scipy\pandas-joris\pandas\core\indexing.pyc in _multi_take(sel
f, tup)
    726             return o.reindex(**d)
    727         except:
--> 728             raise self._exception
    729
    730     def _convert_for_reindex(self, key, axis=0):

KeyError:
```



Discussion:

Resolving patch:
diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py
--- a/pandas/core/indexing.py
+++ b/pandas/core/indexing.py
@@ -710,6 +710,8 @@ def _multi_take_opportunity(self, tup):
                 return False
             elif com._is_bool_indexer(indexer):
                 return False
+            elif not ax.is_unique:
+                return False
 
         return True
 


Test patch:
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
@@ -1599,6 +1599,21 @@ def test_dups_fancy_indexing(self):
         result = df.ix[:,['A','B','C']]
         assert_frame_equal(result, expected)
 
+        # GH 6504, multi-axis indexing
+        df = DataFrame(np.random.randn(9,2), index=[1,1,1,2,2,2,3,3,3], columns=['a', 'b'])
+
+        expected = df.iloc[0:6]
+        result = df.loc[[1, 2]]
+        assert_frame_equal(result, expected)
+
+        expected = df
+        result = df.loc[:,['a', 'b']]
+        assert_frame_equal(result, expected)
+
+        expected = df.iloc[0:6,:]
+        result = df.loc[[1, 2], ['a', 'b']]
+        assert_frame_equal(result, expected)
+
     def test_indexing_mixed_frame_bug(self):
 
         # GH3492