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

MultiIndex partial slicing error #1352

Closed
adamklein opened this issue May 30, 2012 · 1 comment

Comments

@adamklein
Copy link
Contributor

commented May 30, 2012

In [10]: idx = MultiIndex(labels=[[0,0,0],[0,1,1],[1,0,1]],levels=[['a','b'],['x','y'],['p','q']])

In [11]: df = DataFrame(np.random.rand(3,2),index=idx)

In [12]: df.ix[('a','y')]
Out[12]: 
      0         1
p  0.788191  0.781931
q  0.566332  0.486645

In [13]: df[('a','y')]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/home/adam/code/qplib/<ipython-input-13-d3c63a882cb1> in <module>()
----> 1 df[('a','y')]

/home/adam/code/pandas/pandas/core/frame.pyc in __getitem__(self, key)
   1655             return self._getitem_multilevel(key)
   1656         else:
-> 1657             return self._get_item_cache(key)
   1658 
   1659     def _getitem_array(self, key):

/home/adam/code/pandas/pandas/core/generic.pyc in _get_item_cache(self, item)
    472         except Exception:
    473             try:
--> 474                 values = self._data.get(item)
    475                 res = self._box_item_values(item, values)
    476                 cache[item] = res

/home/adam/code/pandas/pandas/core/internals.pyc in get(self, item)
    756 
    757     def get(self, item):
--> 758         _, block = self._find_block(item)
    759         return block.get(item)
    760 

/home/adam/code/pandas/pandas/core/internals.pyc in _find_block(self, item)
    846 
    847     def _find_block(self, item):
--> 848         self._check_have(item)
    849         for i, block in enumerate(self.blocks):
    850             if item in block:

/home/adam/code/pandas/pandas/core/internals.pyc in _check_have(self, item)
    853     def _check_have(self, item):
    854         if item not in self.items:
--> 855             raise KeyError('no item named %s' % str(item))
    856 
    857     def reindex_axis(self, new_axis, method=None, axis=0, copy=True):

KeyError: "no item named ('a', 'y')"

In [14]: df.ix[('a','y'),:]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/home/adam/code/qplib/<ipython-input-14-19a9d60d6a3b> in <module>()
----> 1 df.ix[('a','y'),:]

/home/adam/code/pandas/pandas/core/indexing.pyc in __getitem__(self, key)
     31                 pass
/home/adam/code/pandas/pandas/core/indexing.pyc in _getitem_tuple(self, tup)
     96     def _getitem_tuple(self, tup):
     97         try:
---> 98             return self._getitem_lowerdim(tup)
     99         except IndexingError:
    100             pass

/home/adam/code/pandas/pandas/core/indexing.pyc in _getitem_lowerdim(self, tup)
    165         if isinstance(ax0, MultiIndex):
    166             try:
--> 167                 return self._get_label(tup, axis=0)
    168             except TypeError:
    169                 # slices are unhashable


/home/adam/code/pandas/pandas/core/indexing.pyc in _get_label(self, label, axis)
     39             return self.obj.xs(label, axis=axis, copy=False)
     40         except Exception:
---> 41             return self.obj.xs(label, axis=axis, copy=True)
     42 
     43     def _slice(self, obj, axis=0):

/home/adam/code/pandas/pandas/core/frame.pyc in xs(self, key, axis, level, copy)
   1894         index = self.index
   1895         if isinstance(index, MultiIndex):
-> 1896             loc, new_index = self.index.get_loc_level(key)
   1897         else:
   1898             loc = self.index.get_loc(key)

/home/adam/code/pandas/pandas/core/index.pyc in get_loc_level(self, key, level)
   2000                 for i, k in enumerate(key):
   2001                     if not isinstance(k, slice):
-> 2002                         k = self._get_level_indexer(k, level=i)
   2003                         if isinstance(k, slice):
   2004                             # everything


/home/adam/code/pandas/pandas/core/index.pyc in _get_level_indexer(self, key, level)
   2030     def _get_level_indexer(self, key, level=0):
   2031         level_index = self.levels[level]
-> 2032         loc = level_index.get_loc(key)
   2033         labels = self.labels[level]
   2034 

/home/adam/code/pandas/pandas/core/index.pyc in get_loc(self, key)
    623         loc : int
    624         """
--> 625         return self._engine.get_loc(key)
    626 
    627     def get_value(self, series, key):

/home/adam/code/pandas/pandas/lib.so in pandas.lib.IndexEngine.get_loc (pandas/src/tseries.c:112968)()

/home/adam/code/pandas/pandas/lib.so in pandas.lib.IndexEngine.get_loc (pandas/src/tseries.c:112835)()

/home/adam/code/pandas/pandas/lib.so in pandas.lib.PyObjectHashTable.get_item (pandas/src/tseries.c:22698)()

/home/adam/code/pandas/pandas/lib.so in pandas.lib.PyObjectHashTable.get_item (pandas/src/tseries.c:22652)()

KeyError: ('a', 'y')

@ghost ghost assigned wesm Jun 3, 2012

@wesm wesm closed this in 911fec2 Jun 3, 2012

@wesm

This comment has been minimized.

Copy link
Member

commented Jun 3, 2012

Fixed this. Note that df[('a','y')] wouldn't be expected to work, but the others, yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.