Skip to content

Commit

Permalink
BUG: unordered dup index does not yield scalar value #1586
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang She authored and wesm committed Jul 11, 2012
1 parent 58b5afa commit fff7b89
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pandas/src/engines.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ cdef class IndexEngine:

if self.is_monotonic:
values = self._get_index_values()

left = values.searchsorted(val, side='left')
right = values.searchsorted(val, side='right')

Expand All @@ -149,14 +148,15 @@ cdef class IndexEngine:
else:
return slice(left, right)
else:
return self._get_bool_indexer(val)
return self._maybe_get_bool_indexer(val)

cdef _get_bool_indexer(self, object val):
cdef _maybe_get_bool_indexer(self, object val):
cdef:
ndarray[uint8_t] indexer
ndarray[object] values
int count = 0
Py_ssize_t i, n
int last_true

values = self._get_index_values()
n = len(values)
Expand All @@ -168,11 +168,14 @@ cdef class IndexEngine:
if values[i] == val:
count += 1
indexer[i] = 1
last_true = i
else:
indexer[i] = 0

if count == 0:
raise KeyError(val)
if count == 1:
return last_true

return result

Expand Down Expand Up @@ -275,13 +278,14 @@ cdef class Int64Engine(IndexEngine):
return _algos.backfill_int64(self._get_index_values(), other,
limit=limit)

cdef _get_bool_indexer(self, object val):
cdef _maybe_get_bool_indexer(self, object val):
cdef:
ndarray[uint8_t, cast=True] indexer
ndarray[int64_t] values
int count = 0
Py_ssize_t i, n
int64_t ival
int last_true

if not util.is_integer_object(val):
raise KeyError(val)
Expand All @@ -298,11 +302,14 @@ cdef class Int64Engine(IndexEngine):
if values[i] == val:
count += 1
indexer[i] = 1
last_true = i
else:
indexer[i] = 0

if count == 0:
raise KeyError(val)
if count == 1:
return last_true

return result

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,11 @@ def test_get_loc_duplicates(self):
assert(result == expected)
# self.assertRaises(Exception, index.get_loc, 2)

index = Index(['c', 'a', 'a', 'b', 'b'])
rs = index.get_loc('c')
xp = 0
assert(rs == xp)

def test_get_loc_level(self):
index = MultiIndex(levels=[Index(range(4)),
Index(range(4)),
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,11 @@ def test_getitem_ambiguous_keyerror(self):
self.assertRaises(KeyError, s.__getitem__, 1)
self.assertRaises(KeyError, s.ix.__getitem__, 1)

def test_getitem_unordered_dup(self):
obj = Series(range(5), index=['c', 'a', 'a', 'b', 'b'])
self.assert_(np.isscalar(obj['c']))
self.assert_(obj['c'] == 0)

def test_setitem_ambiguous_keyerror(self):
s = Series(range(10), index=range(0, 20, 2))
self.assertRaises(KeyError, s.__setitem__, 1, 5)
Expand Down

0 comments on commit fff7b89

Please sign in to comment.