-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: loc should not fallback for integer indexing for multi-index #5420
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
Comments
Related from that discussion: |
oh it does, but right now iirc it delegates multi index handling to the ix routines (which do fallback) |
No it doesn't (that was part of what was confusing on ML): In [3]: df = DataFrame({"A": [1, 2, 3]})
In [4]: df
Out[4]:
A
0 1
1 2
2 3
In [6]: df.loc[['a', 'b']]
Out[6]:
A
a NaN
b NaN
In [7]: df.loc['a']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-7-5dbae926782f> in <module>()
----> 1 df.loc['a']
../pandas/pandas/core/indexing.pyc in __getitem__(self, key)
958 return self._getitem_tuple(key)
959 else:
--> 960 return self._getitem_axis(key, axis=0)
961
962 def _getitem_axis(self, key, axis=0):
../pandas/pandas/core/indexing.pyc in _getitem_axis(self, key, axis)
1067 return self._getitem_iterable(key, axis=axis)
1068 else:
-> 1069 self._has_valid_type(key,axis)
1070 return self._get_label(key, axis=axis)
1071
../pandas/pandas/core/indexing.pyc in _has_valid_type(self, key, axis)
1047 raise
1048 except:
-> 1049 error()
1050
1051 return True
../pandas/pandas/core/indexing.pyc in error()
1034 if isnull(key):
1035 raise ValueError("cannot use label indexing with a null key")
-> 1036 raise KeyError("the label [%s] is not in the [%s]" % (key,self.obj._get_axis_name(axis)))
1037
1038 try:
KeyError: 'the label [a] is not in the [index]' |
your example is as expected a single loc raises while a list does not |
Okay, then we should update the docs to reflect that. It's weird that |
|
It clearly has to scan the entire index for each element anyways, right? |
no its doing a hash lookup, |
why would a label not be in the hash if it's in the index? |
not sure I understand the question? it would be in the hash if it's in the index but remember that's only calculated once it doesn't scan when doing lookups just hits the index for the locs |
If I say, df.loc[["A", "B", "C"]], it has to do a hash lookup for each of "A", "B", "C". If it's not in the hash, then it's pretty obvious it's not in the index - right? So this would just be something like: if (ind.get_indexer(<whatever>) == -1).any():
raise KeyError(...) |
not sure I understand the question that is true but what's the point? |
from the docs:
|
not in a list though (guess the docs need to be updated) |
yeah, now we're on the same page |
This is closed by multi-index slicers: http://pandas-docs.github.io/pandas-docs-travis/whatsnew.html#multiindexing-using-slicers |
https://groups.google.com/forum/m/#!topic/pydata/W0e3l0UvNwI
The text was updated successfully, but these errors were encountered: