Skip to content

Bug indexing a DataFrame with MultiIndex through .ix #2314

@manuteleco

Description

@manuteleco

Hi,

here is an example that demonstrates the error I'm getting (using pandas 0.9.2.dev-61766ec):

Example script:

from pandas import DataFrame, MultiIndex

index_columns = list("abc")
df = DataFrame([[0, 1, 0, "x"], [0, 0, 1, "y"]],
               columns=index_columns + ["data"])
df = df.set_index(index_columns)
print "Test DataFrame:"
print df
print

query_index = df.index[:1]  # first index
print "MultiIndex to index the DataFrame through .ix[...]:"
print repr(query_index)
print

print "This works:"
print df.ix[query_index]["data"]
print

print "This doesn't:"
print df.ix[query_index, "data"]
print

which outputs:

Test DataFrame:
      data
a b c     
0 1 0    x
  0 1    y

MultiIndex to index the DataFrame through .ix[...]:
MultiIndex
[(0, 1, 0)]

This works:
a  b  c
0  1  0    x
Name: data

This doesn't:
Traceback (most recent call last):
  File "pandas_bug_github.py", line 20, in <module>
    print df.ix[query_index, "data"]
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev_61766ec-py2.7-linux-x86_64.egg/pandas/core/indexing.py", line 32, in __getitem__
    return self._getitem_tuple(key)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev_61766ec-py2.7-linux-x86_64.egg/pandas/core/indexing.py", line 202, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev_61766ec-py2.7-linux-x86_64.egg/pandas/core/indexing.py", line 287, in _getitem_lowerdim
    loc = ax0.get_loc(tup[0])
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev_61766ec-py2.7-linux-x86_64.egg/pandas/core/index.py", line 2080, in get_loc
    return self._get_level_indexer(key, level=0)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev_61766ec-py2.7-linux-x86_64.egg/pandas/core/index.py", line 2182, in _get_level_indexer
    loc = level_index.get_loc(key)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev_61766ec-py2.7-linux-x86_64.egg/pandas/core/index.py", line 674, in get_loc
    return self._engine.get_loc(key)
  File "engines.pyx", line 113, in pandas.lib.IndexEngine.get_loc (pandas/src/tseries.c:124894)
  File "engines.pyx", line 115, in pandas.lib.IndexEngine.get_loc (pandas/src/tseries.c:124554)
TypeError

The funny thing is that if, for instance, we swap the rows in the dataframe ( [[0, 0, 1, "y"], [0, 1, 0, "x"]] instead of [[0, 1, 0, "x"], [0, 0, 1, "y"]] ) it works just fine.

Regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions