Repository: pandas-dev/pandas

Issue:
API: selection from a duplicated index of a Series return a Series?
See below for discussion

---

```
In [19]: import pandas as pd

In [20]: import numpy as np

In [21]: import random

In [22]: df = pd.DataFrame(np.random.random_sample((20,5)), index=[random.choice('ABCDE') for x in range(20)])

In [23]: df.loc[:,0].ix['A'].median()
Out[23]: 0.57704085832236685

In [24]: pd.version.version
Out[24]: '0.12.0'


In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: import random

In [4]: df = pd.DataFrame(np.random.random_sample((20,5)), index=[random.choice('ABCDE') for x in range(20)])

In [5]: df.loc[:,0].ix['A'].median()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-57f9fc9d1583> in <module>()
----> 1 df.loc[:,0].ix['A'].median()

AttributeError: 'numpy.ndarray' object has no attribute 'median'

In [6]: pd.version.version
Out[6]: '0.13.0rc1-43-g4f9fefc'
```



Discussion:
median works correctly.

```
In [5]: df.apply(lambda x: x.median())
Out[5]: 
0    0.478124
1    0.540118
2    0.325508
3    0.531052
4    0.537722
dtype: float64

In [6]: df.median()
Out[6]: 
0    0.478124
1    0.540118
2    0.325508
3    0.531052
4    0.537722
dtype: float64
```

```
In [8]: df.loc[:,0]
Out[8]: 
B    0.910850
C    0.495885
C    0.617074
A    0.382164
D    0.335341
B    0.705503
C    0.543724
D    0.742013
C    0.824611
C    0.272631
E    0.773018
C    0.460364
C    0.511589
D    0.662656
E    0.291188
E    0.031795
A    0.025877
D    0.127890
B    0.457107
C    0.446388
Name: 0, dtype: float64
```

You are selecting out of a duplicated index Series. You could argue that you should bet back another Series, however, if its a unique indexed Series you would be getting back a scalar (a float). I will mark this as a bug, but for the selection of a duplicate from a Series.

```
In [9]: df.loc[:,0].ix['A']
Out[9]: array([ 0.3821636 ,  0.02587671])
```

Select like this.

```
In [10]: df.loc['A',0]
Out[10]: 
A    0.382164
A    0.025877
Name: 0, dtype: float64
```


Resolving patch:
diff --git a/pandas/core/series.py b/pandas/core/series.py
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -481,7 +481,10 @@ def _slice(self, slobj, axis=0, raise_on_error=False, typ=None):
 
     def __getitem__(self, key):
         try:
-            return self.index.get_value(self, key)
+            result = self.index.get_value(self, key)
+            if isinstance(result, np.ndarray):
+                return self._constructor(result,index=[key]*len(result)).__finalize__(self)
+            return result
         except InvalidIndexError:
             pass
         except (KeyError, ValueError):


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
@@ -316,6 +316,14 @@ def test_at_timestamp(self):
     def test_iat_invalid_args(self):
         pass
 
+    def test_repeated_getitem_dups(self):
+        # GH 5678
+        # repeated gettitems on a dup index returing a ndarray
+        df = DataFrame(np.random.random_sample((20,5)), index=['ABCDE'[x%5] for x in range(20)])
+        expected = df.loc['A',0]
+        result = df.loc[:,0].loc['A']
+        assert_series_equal(result,expected)
+
     def test_iloc_getitem_int(self):
 
         # integer