Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug indexing a DataFrame with MultiIndex through .ix #2314

Closed
manuteleco opened this issue Nov 21, 2012 · 4 comments
Closed

Bug indexing a DataFrame with MultiIndex through .ix #2314

manuteleco opened this issue Nov 21, 2012 · 4 comments
Milestone

Comments

@manuteleco
Copy link

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.

@gerigk
Copy link

gerigk commented Nov 23, 2012

I also came across this behavior but wasn't able to extract a self contained example. It seems kind of crazy to me that

from pandas import DataFrame, MultiIndex

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

works but

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)
query_index = df.index[:1] 
df.ix[query_index, "data"]

doesn't.

@changhiskhan
Copy link
Contributor

That's unfortunately because parts of the internal machinery in Index treats sorted vs unsorted differently. I added a fix for this. Please test and let me know if it works for you. Thanks for the bug report!

@gerigk
Copy link

gerigk commented Dec 1, 2012

it works for me now. thanks a lot! @manuteleco does it work for you, too?

@wesm
Copy link
Member

wesm commented Dec 2, 2012

Looks good to me. Closing until anyone finds another problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants