-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
API: generalized check_array_indexer for validating array-like getitem indexers #31150
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 1 commit
e8f539a
4fa9f5a
b55dfd2
095b741
58bfe78
5ce8d85
ebc2150
4a51d97
50490aa
c979df8
ce2e042
4d447bf
d930e84
9ed8fe9
2f8cd27
4d9a201
097d221
3c5e4c6
1ca35d1
e5ea9b4
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 |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| is_datetime64_any_dtype, | ||
| is_dtype_equal, | ||
| is_integer, | ||
| is_list_like, | ||
| is_object_dtype, | ||
| is_scalar, | ||
| is_string_dtype, | ||
|
|
@@ -43,6 +44,7 @@ | |
| from pandas.core.base import PandasObject | ||
| import pandas.core.common as com | ||
| from pandas.core.construction import sanitize_array | ||
| from pandas.core.indexers import check_array_indexer | ||
| from pandas.core.missing import interpolate_2d | ||
| import pandas.core.ops as ops | ||
| from pandas.core.ops.common import unpack_zerodim_and_defer | ||
|
|
@@ -768,6 +770,9 @@ def __getitem__(self, key): | |
| else: | ||
| key = np.asarray(key) | ||
|
|
||
| if is_list_like(key): | ||
|
||
| key = check_array_indexer(self, key) | ||
|
|
||
| if com.is_bool_indexer(key): | ||
| key = check_bool_indexer(self, key) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,12 @@ | |
|
|
||
| from pandas._typing import AnyArrayLike | ||
|
|
||
| from pandas.core.dtypes.common import is_list_like | ||
| from pandas.core.dtypes.common import ( | ||
| is_array_like, | ||
| is_bool_dtype, | ||
| is_integer_dtype, | ||
| is_list_like, | ||
| ) | ||
| from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries | ||
|
|
||
| # ----------------------------------------------------------- | ||
|
|
@@ -307,3 +312,62 @@ def check_bool_array_indexer(array: AnyArrayLike, mask: AnyArrayLike) -> np.ndar | |
| if len(result) != len(array): | ||
| raise IndexError(f"Item wrong length {len(result)} instead of {len(array)}.") | ||
| return result | ||
|
|
||
|
|
||
| def check_array_indexer(array, indexer) -> np.ndarray: | ||
|
||
| """ | ||
| Check if `indexer` is a valid array indexer for `array`. | ||
|
|
||
| `array` and `indexer` are checked to have the same length, and the | ||
| dtype is validated. If it is an integer or boolean ExtensionArray, it is | ||
| checked if there are missing values present, and it is converted to | ||
| the appropriate numpy array. | ||
|
|
||
| .. versionadded:: 1.0.0 | ||
|
||
|
|
||
| Parameters | ||
| ---------- | ||
| array : array | ||
|
||
| The array that's being indexed (only used for the length). | ||
| indexer : array-like | ||
|
||
| The array-like that's used to index. | ||
|
|
||
| Returns | ||
| ------- | ||
| numpy.ndarray | ||
| The validated indexer. | ||
|
|
||
| Raises | ||
| ------ | ||
| IndexError | ||
| When the lengths don't match. | ||
| ValueError | ||
| When `indexer` cannot be converted to a numpy ndarray. | ||
|
|
||
| """ | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import pandas as pd | ||
|
|
||
| if not is_array_like(indexer): | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| indexer = pd.array(indexer) | ||
| dtype = indexer.dtype | ||
| if is_bool_dtype(dtype): | ||
| try: | ||
| indexer = np.asarray(indexer, dtype=bool) | ||
| except ValueError: | ||
| raise ValueError("Cannot mask with a boolean indexer containing NA values") | ||
|
|
||
| # GH26658 | ||
| if len(indexer) != len(array): | ||
| raise IndexError( | ||
| f"Item wrong length {len(indexer)} instead of {len(array)}." | ||
| ) | ||
|
|
||
| elif is_integer_dtype(dtype): | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try: | ||
| indexer = np.asarray(indexer, dtype=int) | ||
|
||
| except ValueError: | ||
| raise ValueError( | ||
| "Cannot index with an integer indexer containing NA values" | ||
| ) | ||
|
|
||
| return indexer | ||
Uh oh!
There was an error while loading. Please reload this page.