-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: specificy missing labels in loc calls GH34272 #34912
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
ENH: specificy missing labels in loc calls GH34272 #34912
Conversation
Added what's new documentation.
…h-specificy-missing-labels-34272
Flake 8 line length linting.
This should address #34272. Would it be useful to add max limit to the number of missing labels it would print? I think this will be handled better by the str method of the Series class, already. |
Fix ipython directive to code block so that error message can be included.
doc/source/whatsnew/v1.1.0.rst
Outdated
.. code-block:: ipython | ||
|
||
s = pd.Series({"a": 1, "b": 2, "c": 3}) | ||
s.loc[["a", "b", "missing_0", "c", "missing_1", "missing_2"]] |
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 don’t need a section for this just a note in the whats new
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.
sure, I've removed the code block and just left the text entry in the what's new.
pandas/core/indexing.py
Outdated
@@ -1302,10 +1303,12 @@ def _validate_read_indexer( | |||
# code, so we want to avoid warning & then | |||
# just raising | |||
if not ax.is_categorical(): | |||
not_found = list(key[missing_mask]) |
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.
instead of doing this you can create an Index which has the natural formatters
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.
Done, and I now also use the option context to limit the number of items displayed.
Note, the if raise_missing
block just above this uses a list to print the missing keys here. Independently of this PR, if raise_missing==False, an error would be raised directly below regardless (unless the axis is categorical). Not sure if this is the expected/desired behaviour? Perhaps one for another ticket/PR.
@@ -1075,3 +1075,12 @@ def test_setitem_with_bool_mask_and_values_matching_n_trues_in_length(): | |||
result = ser | |||
expected = pd.Series([None] * 3 + list(range(5)) + [None] * 2).astype("object") | |||
tm.assert_series_equal(result, expected) | |||
|
|||
|
|||
def test_missing_labels_inside_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 u also try with a lot of missing labels (testing that message is constrained)
you will need to set the max line width via option context
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.
Good point, done. I added a test for many labels and a test for very long labels.
Note because we are raising a KeyError the message is wrapped in quotes (see stack overflow and https://bugs.python.org/issue2651 . This means the \n characters appear in the message.
The pre-existing error message text here is also quite long as it includes a link to the docs and text on "no longer supported". Let me know if you think it should be shortened.
Fixed rst flake8 issues.
PR feedback: what's new code block removed; additional test for many or long missing labels, context manager for display options.
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.
small comments ping on green
pandas/core/indexing.py
Outdated
"https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501 | ||
) | ||
not_found = key[missing_mask] | ||
from pandas import option_context |
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.
import at the top
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.
done
Previously, if labels were missing for a loc call, a Key Error was raised stating that this was no longer supported. | ||
|
||
Now the error message also includes a list of the missing labels (max 10 items, display width 80 characters). | ||
|
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 u add the issue number 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.
done
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 do it like other issue references, e.g. :issue:`34272`
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.
@jreback - sure, this is now done.
PR feedback: reference issue in what's new and import position.
PR feedback: import sorting (isort) fix
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.
ping when green
Previously, if labels were missing for a loc call, a Key Error was raised stating that this was no longer supported. | ||
|
||
Now the error message also includes a list of the missing labels (max 10 items, display width 80 characters). | ||
|
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 do it like other issue references, e.g. :issue:`34272`
PR feedback: add issue tag
PR feedback: minor edit of what is new message.
thanks @timhunderwood very nice |
thanks, Happy to help! |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff