-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
DOC/TST: Indexing with NA raises #30308
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 18 commits
492f904
6444aa0
53f4f63
3bbf868
a5ac457
0dfe761
dac111d
d1f08d9
3dd59ca
151bdfe
d57b0ac
36be0f6
7bd6c2f
c5f3afb
76bb6ce
505112e
c73ae8e
3efe359
f94483f
953938d
8b1e567
f317c64
c656292
d4f0adc
37ea95e
816a47c
21fd589
3637070
61599f2
6a0eda6
e622826
5004d91
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
"""Public API for Rolling Window Indexers""" | ||
from pandas.core.indexers import check_bool_array_indexer # noqa: F401 | ||
from pandas.core.window.indexers import BaseIndexer # noqa: F401 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
""" | ||
import numpy as np | ||
|
||
from pandas._typing import AnyArrayLike | ||
|
||
from pandas.core.dtypes.common import is_list_like | ||
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries | ||
|
||
|
@@ -240,3 +242,68 @@ def length_of_indexer(indexer, target=None) -> int: | |
elif not is_list_like_indexer(indexer): | ||
return 1 | ||
raise AssertionError("cannot find the length of the indexer") | ||
|
||
|
||
def check_bool_array_indexer(array: AnyArrayLike, mask: AnyArrayLike) -> np.ndarray: | ||
""" | ||
Check if `mask` is a valid boolean indexer for `array`. | ||
|
||
`array` and `mask` are checked to have the same length, and the | ||
dtype is validated. | ||
|
||
.. versionadded:: 1.0.0 | ||
|
||
Parameters | ||
---------- | ||
array : array | ||
The array that's being masked. | ||
mask : array | ||
The boolean array that's masking. | ||
|
||
Returns | ||
------- | ||
numpy.ndarray | ||
The validated boolean mask. | ||
|
||
Raises | ||
------ | ||
IndexError | ||
When the lengths don't match. | ||
ValueError | ||
When `mask` cannot be converted to a bool-dtype ndarray. | ||
|
||
See Also | ||
-------- | ||
api.extensions.is_bool_indexer : Check if `key` is a boolean indexer. | ||
|
||
Examples | ||
-------- | ||
A boolean ndarray is returned when the arguments are all valid. | ||
|
||
>>> mask = pd.array([True, False]) | ||
>>> arr = pd.Series([1, 2]) | ||
>>> pd.api.extensions.check_bool_array_indexer(arr, mask) | ||
array([ True, False]) | ||
|
||
An IndexError is raised when the lengths don't match. | ||
|
||
>>> mask = pd.array([True, False, True]) | ||
>>> pd.api.extensions.check_bool_array_indexer(arr, mask) | ||
Traceback (most recent call last): | ||
... | ||
IndexError: Item wrong length 3 instead of 2. | ||
|
||
A ValueError is raised when the mask cannot be converted to | ||
a bool-dtype ndarray. | ||
|
||
>>> mask = pd.array([True, pd.NA]) | ||
>>> pd.api.extensions.check_bool_array_indexer(arr, mask) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: cannot convert to bool numpy array in presence of missing values | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we try to improve this error message? We know this is called in terms of indexing, and then something like "Cannot do boolean indexing with missing values, use fillna(True/False) ..." would be a much more useful error message than the message about conversion to numpy array. |
||
""" | ||
result = np.asarray(mask, dtype=bool) | ||
# GH26658 | ||
if len(result) != len(array): | ||
raise IndexError(f"Item wrong length {len(result)} instead of {len(array)}.") | ||
return result |
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.
this does not exist now (will do a PR)
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.
#30725