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: .loc on MultiIndex with list of indexers including an incomplete slice raises KeyError #7399

Closed
shoyer opened this issue Jun 9, 2014 · 1 comment · Fixed by #7404
Closed
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Milestone

Comments

@shoyer
Copy link
Member

shoyer commented Jun 9, 2014

Tested on latest version

import pandas as pd
midx = pd.MultiIndex.from_product([range(5), ['a', 'b', 'c']])
s = pd.Series(range(15), midx)
s.loc[:, 'a':'c'] # works
s.loc[0:4, 'a':'c'] # works
s.loc[:4, 'a':'c'] # raises KeyError
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-4865919a19c8> in <module>()
----> 1 s.loc[:4, 'a':'c'] # raises KeyError

/Users/shoyer/dev/pandas/pandas/core/indexing.pyc in __getitem__(self, key)
   1125     def __getitem__(self, key):
   1126         if type(key) is tuple:
-> 1127             return self._getitem_tuple(key)
   1128         else:
   1129             return self._getitem_axis(key, axis=0)

/Users/shoyer/dev/pandas/pandas/core/indexing.pyc in _getitem_tuple(self, tup)
    643     def _getitem_tuple(self, tup):
    644         try:
--> 645             return self._getitem_lowerdim(tup)
    646         except IndexingError:
    647             pass

/Users/shoyer/dev/pandas/pandas/core/indexing.pyc in _getitem_lowerdim(self, tup)
    751         # we may have a nested tuples indexer here
    752         if self._is_nested_tuple_indexer(tup):
--> 753             return self._getitem_nested_tuple(tup)
    754
    755         # we maybe be using a tuple to represent multiple dimensions here

/Users/shoyer/dev/pandas/pandas/core/indexing.pyc in _getitem_nested_tuple(self, tup)
    810
    811             # this is a series with a multi-index specified a tuple of selectors
--> 812             return self._getitem_axis(tup, axis=0, validate_iterable=True)
    813
    814         # handle the multi-axis by taking sections and reducing

/Users/shoyer/dev/pandas/pandas/core/indexing.pyc in _getitem_axis(self, key, axis, validate_iterable)
   1267             # nested tuple slicing
   1268             if _is_nested_tuple(key, labels):
-> 1269                 locs = labels.get_locs(key)
   1270                 indexer = [ slice(None) ] * self.ndim
   1271                 indexer[axis] = locs

/Users/shoyer/dev/pandas/pandas/core/index.pyc in get_locs(self, tup)
   3602             elif isinstance(k,slice):
   3603                 # a slice, include BOTH of the labels
-> 3604                 ranges.append(self._get_level_indexer(k,level=i))
   3605             else:
   3606                 # a single label

/Users/shoyer/dev/pandas/pandas/core/index.pyc in _get_level_indexer(self, key, level)
   3527             # otherwise a boolean indexer
   3528
-> 3529             start = level_index.get_loc(key.start)
   3530             stop  = level_index.get_loc(key.stop)
   3531             step = key.step

/Users/shoyer/dev/pandas/pandas/core/index.pyc in get_loc(self, key)
   1167         loc : int if unique index, possibly slice or mask if not
   1168         """
-> 1169         return self._engine.get_loc(_values_from_object(key))
   1170
   1171     def get_value(self, series, key):

/Users/shoyer/dev/pandas/pandas/index.so in pandas.index.IndexEngine.get_loc (pandas/index.c:3651)()

/Users/shoyer/dev/pandas/pandas/index.so in pandas.index.IndexEngine.get_loc (pandas/index.c:3578)()

KeyError: None
@jreback
Copy link
Contributor

jreback commented Jun 9, 2014

thanks for the reports!

keep em coming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants