Skip to content

Slicing using .loc fails when either boundary falls outside the index #5223

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

Closed
SleepingPills opened this issue Oct 14, 2013 · 3 comments
Closed

Comments

@SleepingPills
Copy link
Contributor

Using version 0.12, is throwing an exception the intended behavior?

>>> s = pd.Series(np.arange(10))
>>> s.loc[:15]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-37-e6f70b3a4e46> in <module>()
----> 1 s.loc[:15]

/Users/sleepingpills/anaconda/envs/research/python.app/Contents/lib/python2.7/site-packages/pandas/core/indexing.pyc in __getitem__(self, key)
    697             return self._getitem_tuple(key)
    698         else:
--> 699             return self._getitem_axis(key, axis=0)
    700 
    701     def _getitem_axis(self, key, axis=0):

/Users/sleepingpills/anaconda/envs/research/python.app/Contents/lib/python2.7/site-packages/pandas/core/indexing.pyc in _getitem_axis(self, key, axis)
    773 
    774         if isinstance(key, slice):
--> 775             self._has_valid_type(key,axis)
    776             return self._get_slice_axis(key, axis=axis)
    777         elif com._is_bool_indexer(key):

/Users/sleepingpills/anaconda/envs/research/python.app/Contents/lib/python2.7/site-packages/pandas/core/indexing.pyc in _has_valid_type(self, key, axis)
    744             if key.stop is not None:
    745                 if key.stop not in ax:
--> 746                     raise KeyError("stop bound [%s] is not in the [%s]" % (key.stop,self.obj._get_axis_name(axis)))
    747 
    748         elif com._is_bool_indexer(key):

KeyError: 'stop bound [15] is not in the [index]'

The .ix indexer works as expected here (and returns the items contained in the slice).

@jreback
Copy link
Contributor

jreback commented Oct 14, 2013

this is as intended

ix actually treats this as a positional indexer but then doesn't complain that it's out of bounds (like a list indexer in python allows u to go over the end)

which is pretty un-intuitive IMHO

the loc prevents these types of issues

@jreback
Copy link
Contributor

jreback commented Oct 15, 2013

@SleepingPills pls close if this is answered

@jreback
Copy link
Contributor

jreback commented Oct 15, 2013

closing as not a bug

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

2 participants