-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
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
Changes from 5 commits
63fbce4
140794b
cde8c59
ac23b02
6b1ad07
8710596
b77dcfc
c460a03
ecaf345
744c828
18d2b83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,20 @@ including other versions of pandas. | |
| Enhancements | ||
| ~~~~~~~~~~~~ | ||
|
|
||
| .. _whatsnew_110.specify_missing_labels: | ||
|
|
||
| KeyErrors raised by loc specify missing labels | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 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. For example, | ||
|
|
||
| .. code-block:: ipython | ||
|
|
||
| s = pd.Series({"a": 1, "b": 2, "c": 3}) | ||
| s.loc[["a", "b", "missing_0", "c", "missing_1", "missing_2"]] | ||
|
||
| ... | ||
| KeyError: "Passing list-likes to .loc or [] with any missing labels is no longer supported. The following labels were missing: ['missing_0', 'missing_1', 'missing_2']. See https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" | ||
| .. _whatsnew_110.astype_string: | ||
|
|
||
| All dtypes can now be converted to ``StringDtype`` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1283,7 +1283,8 @@ def _validate_read_indexer( | |
| return | ||
|
|
||
| # Count missing values: | ||
| missing = (indexer < 0).sum() | ||
| missing_mask = indexer < 0 | ||
| missing = (missing_mask).sum() | ||
|
|
||
| if missing: | ||
| if missing == len(indexer): | ||
|
|
@@ -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]) | ||
|
||
| raise KeyError( | ||
| "Passing list-likes to .loc or [] with any missing labels " | ||
| "is no longer supported, see " | ||
| "https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501 | ||
| "is no longer supported. " | ||
| f"The following labels were missing: {not_found}. " | ||
| "See https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501 | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(): | ||
|
||
| # GH34272 | ||
| s = pd.Series({"a": 1, "b": 2, "c": 3}) | ||
| with pytest.raises(KeyError) as e: | ||
| s.loc[["a", "b", "missing_0", "c", "missing_1", "missing_2"]] | ||
| missing_labels = ["missing_0", "missing_1", "missing_2"] | ||
| assert all(missing_label in str(e.value) for missing_label in missing_labels) | ||
timhunderwood marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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`Uh oh!
There was an error while loading. Please reload this page.
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.