-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix MultiIndex .loc "Too Many Indexers" with None as return value #34450
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. We will want to be more specific than just raising Exception
as that is too generic to be useful, but let's see if we can refactor a bit
pandas/core/indexing.py
Outdated
@@ -1065,7 +1069,7 @@ def _handle_lowerdim_multi_index_axis0(self, tup: Tuple): | |||
if len(tup) <= self.obj.index.nlevels and len(tup) > self.ndim: | |||
raise ek | |||
|
|||
return None | |||
raise Exception("No label returned") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this not already raise a KeyError a few lines up?
# GH34318: test that you can access a None value using .loc through a Multiindex | ||
|
||
expected = None | ||
result = pd.Series([None], pd.MultiIndex.from_arrays([["Level1"], ["Level2"]])).loc[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you write this in a more readable way
s = Series(......)
result = s.loc....
assert result is None
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pls add all the examples here as well: #34318 (comment)
pls also add a whatsnew note, indexing bug fixes for 1.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm ping on green after tests move.
@@ -853,6 +853,23 @@ def test_setitem_slice_into_readonly_backing_data(): | |||
assert not array.any() | |||
|
|||
|
|||
def test_access_none_value_in_multiindex(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make a new file in this dir: test_multiindex.py
we have the MI tests elsewhere so ok starting a new file here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should I copy all the imports from test_indexing.py?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need whatever imports make flake happy :->
from pandas import ( | ||
Categorical, | ||
DataFrame, | ||
IndexSlice, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are going to fail linting here. see https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#python-pep8-black
for how to install pre-commit hooks to avoid this issue
thanks @pedrooa |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
Three changes in indexing.py :