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: strange behavior of .loc indexer: falling back to integer-based indexing with list #7496

Closed
code-of-kpp opened this issue Jun 18, 2014 · 3 comments · Fixed by #7497
Closed
Labels
API Design Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@code-of-kpp
Copy link

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> a = pandas.Series()
>>> a.loc[1] = 1
>>> a.loc['a'] = 2
>>> a.loc[[-1, -2]]
1    1
a    2
dtype: int64

This is ok:

>>> a.loc[-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/venv/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1129, in __getitem__
    return self._getitem_axis(key, axis=0)
  File "/tmp/venv/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1261, in _getitem_axis
    self._has_valid_type(key, axis)
  File "/tmp/venv/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1234, in _has_valid_type
    error()
  File "/tmp/venv/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1221, in error
    (key, self.obj._get_axis_name(axis)))
KeyError: 'the label [-1] is not in the [index]'

This is ok too:

>>> a.loc[['W']]
W   NaN
dtype: float64

But this is not:

>>> a.loc[-1] = 3
>>> a.loc[[-1, -2]]
-1    3
1    1
dtype: int64

And this is not good at all:

>>> a
1    1
-1    3
dtype: int64
>>> a['a'] = 2
>>> a
1     1
-1    3
a     2
dtype: int64
>>> a.loc[[-2]] = 0
>>> a
1     0
-1    3
a     2
dtype: int64

Without 'a' string in the index it raises while I expect new item ({-2: 0}) to be added

>>> del a['a']
>>> a.loc[[-2]] = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/venv/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 117, in __setitem__
    indexer = self._convert_to_indexer(key, is_setter=True)
  File "/tmp/venv/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1068, in _convert_to_indexer
    raise KeyError('%s not in index' % objarr[mask])
KeyError: '[-2] not in index'
@jreback
Copy link
Contributor

jreback commented Jun 18, 2014

hmm.

must not be some quite strict checking; in fact the negative indexers should raise as well (your 2nd example).

@jreback jreback added this to the 0.14.1 milestone Jun 18, 2014
@code-of-kpp
Copy link
Author

2nd example contains indexing with list of strings
Probably it is good idea to replace '4' with 'W'

@jreback
Copy link
Contributor

jreback commented Jun 18, 2014

fixed in #7497

any more examples to include?

very subtle issue!

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