-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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 all 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 |
---|---|---|
|
@@ -29,6 +29,8 @@ | |
from pandas.core import nanops, ops | ||
from pandas.core.algorithms import take | ||
from pandas.core.arrays import ExtensionArray, ExtensionOpsMixin | ||
import pandas.core.common as com | ||
from pandas.core.indexers import check_bool_array_indexer | ||
|
||
if TYPE_CHECKING: | ||
from pandas._typing import Scalar | ||
|
@@ -307,11 +309,22 @@ def _from_factorized(cls, values, original: "BooleanArray"): | |
def _formatter(self, boxed=False): | ||
return str | ||
|
||
@property | ||
def _hasna(self) -> bool: | ||
# Note: this is expensive right now! The hope is that we can | ||
# make this faster by having an optional mask, but not have to change | ||
# source code using it.. | ||
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. this could easily be cached (and then updated on setitem / other mutation) |
||
return self._mask.any() | ||
|
||
def __getitem__(self, item): | ||
if is_integer(item): | ||
if self._mask[item]: | ||
return self.dtype.na_value | ||
return self._data[item] | ||
|
||
elif com.is_bool_indexer(item): | ||
item = check_bool_array_indexer(self, item) | ||
|
||
return type(self)(self._data[item], self._mask[item]) | ||
|
||
def _coerce_to_ndarray(self, dtype=None, na_value: "Scalar" = libmissing.NA): | ||
|
@@ -329,7 +342,7 @@ def _coerce_to_ndarray(self, dtype=None, na_value: "Scalar" = libmissing.NA): | |
if dtype is None: | ||
dtype = object | ||
if is_bool_dtype(dtype): | ||
if not self.isna().any(): | ||
if not self._hasna: | ||
return self._data | ||
else: | ||
raise ValueError( | ||
|
@@ -503,7 +516,7 @@ def astype(self, dtype, copy=True): | |
|
||
if is_bool_dtype(dtype): | ||
# astype_nansafe converts np.nan to True | ||
if self.isna().any(): | ||
if self._hasna: | ||
raise ValueError("cannot convert float NaN to bool") | ||
else: | ||
return self._data.astype(dtype, copy=copy) | ||
|
@@ -515,7 +528,7 @@ def astype(self, dtype, copy=True): | |
) | ||
# for integer, error if there are missing values | ||
if is_integer_dtype(dtype): | ||
if self.isna().any(): | ||
if self._hasna: | ||
raise ValueError("cannot convert NA to integer") | ||
# for float dtype, ensure we use np.nan before casting (numpy cannot | ||
# deal with pd.NA) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -738,6 +738,9 @@ def value_counts(self, dropna=True): | |
# -------- | ||
|
||
def __getitem__(self, key): | ||
# avoid mypy issues when importing at the top-level | ||
from pandas.core.indexing import check_bool_indexer | ||
|
||
if isinstance(key, tuple): | ||
if len(key) > 1: | ||
raise IndexError("too many indices for array.") | ||
|
@@ -766,7 +769,9 @@ def __getitem__(self, key): | |
else: | ||
key = np.asarray(key) | ||
|
||
if com.is_bool_indexer(key) and len(self) == len(key): | ||
if com.is_bool_indexer(key): | ||
key = check_bool_indexer(self, key) | ||
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. why is this check_bool_indexer when in all the others its check_bool_array_indexer? found this bc mypy is complaining about that the first arg should be an Index |
||
|
||
return self.take(np.arange(len(key), dtype=np.int32)[key]) | ||
elif hasattr(key, "__len__"): | ||
return self.take(key) | ||
|
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. | ||
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. this does not exist now (will do a PR) 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. |
||
|
||
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 |
Uh oh!
There was an error while loading. Please reload this page.