Skip to content

BUG: unexpected KeyError with Index.get_loc #11201

@liuyu81

Description

@liuyu81

Index.get_loc raises unexpected KeyError when data is a 2-dimensional numpy.ndarray with non-unique, monotonically increasing values, i.e.,

>>> import numpy as np
>>> import pandas as pd
>>> ix = pd.Index(np.array([["A"], ["A"], ["B"]]))
>>> ix.get_loc(ix[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/pandas/core/index.py", line 1572, in get_loc
    return self._engine.get_loc(_values_from_object(key))
  File "pandas/index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas/index.c:3824)
  File "pandas/index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas/index.c:3651)
  File "pandas/index.pyx", line 169, in pandas.index.IndexEngine._get_loc_duplicates (pandas/index.c:3990)
KeyError: ('A',)

But it works correctly when the Index is either unique, i.e.,

>>> ix = pd.Index(np.array([["A"], ["B"], ["C"]]))
>>> ix.get_loc(ix[0])
0

or non-monotonic, i.e.,

>>> ix = pd.Index(np.array([["A"], ["B"], ["A"]]))
>>> ix.get_loc(ix[0])
array([ True, False,  True], dtype=bool)

p.s. pandas.__version__ 0.16.2; numpy.__version__ 1.9.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions