Skip to content

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

Merged
merged 32 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
492f904
DOC/TST: Indexing with NA raises
TomAugspurger Dec 16, 2019
6444aa0
Merge remote-tracking branch 'upstream/master' into na-indexing-raises
TomAugspurger Dec 18, 2019
53f4f63
Handle BooleanArray in all EAs
TomAugspurger Dec 18, 2019
3bbf868
update
TomAugspurger Dec 18, 2019
a5ac457
fixups
TomAugspurger Dec 18, 2019
0dfe761
type
TomAugspurger Dec 18, 2019
dac111d
fix benchmark
TomAugspurger Dec 18, 2019
d1f08d9
fixup
TomAugspurger Dec 18, 2019
3dd59ca
typo
TomAugspurger Dec 18, 2019
151bdfe
updates
TomAugspurger Dec 19, 2019
d57b0ac
Revert "updates"
TomAugspurger Dec 19, 2019
36be0f6
examples
TomAugspurger Dec 20, 2019
7bd6c2f
restore datetime fix
TomAugspurger Dec 20, 2019
c5f3afb
Merge remote-tracking branch 'upstream/master' into na-indexing-raises
TomAugspurger Dec 20, 2019
76bb6ce
Merge branch 'master' of https://github.com/pandas-dev/pandas into na…
TomAugspurger Dec 28, 2019
505112e
update error message
TomAugspurger Dec 28, 2019
c73ae8e
checks
TomAugspurger Dec 28, 2019
3efe359
Merge remote-tracking branch 'upstream/master' into na-indexing-raises
TomAugspurger Dec 30, 2019
f94483f
update for error message
TomAugspurger Dec 30, 2019
953938d
Merge remote-tracking branch 'upstream/master' into na-indexing-raises
TomAugspurger Dec 30, 2019
8b1e567
update isort
TomAugspurger Dec 30, 2019
f317c64
isort
TomAugspurger Dec 30, 2019
c656292
fixup
TomAugspurger Dec 30, 2019
d4f0adc
Merge branch 'master' of https://github.com/pandas-dev/pandas into na…
TomAugspurger Dec 31, 2019
37ea95e
fixup
TomAugspurger Dec 31, 2019
816a47c
Merge remote-tracking branch 'upstream/master' into na-indexing-raises
TomAugspurger Jan 2, 2020
21fd589
update arrayo
TomAugspurger Jan 2, 2020
3637070
doc
TomAugspurger Jan 2, 2020
61599f2
integer
TomAugspurger Jan 2, 2020
6a0eda6
Merge remote-tracking branch 'upstream/master' into na-indexing-raises
TomAugspurger Jan 2, 2020
e622826
fixup
TomAugspurger Jan 2, 2020
5004d91
fixup
TomAugspurger Jan 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions asv_bench/benchmarks/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def setup(self):
self.col_scalar = columns[10]
self.bool_indexer = self.df[self.col_scalar] > 0
self.bool_obj_indexer = self.bool_indexer.astype(object)
self.boolean_indexer = (self.df[self.col_scalar] > 0).astype("boolean")

def time_loc(self):
self.df.loc[self.idx_scalar, self.col_scalar]
Expand All @@ -144,6 +145,9 @@ def time_boolean_rows(self):
def time_boolean_rows_object(self):
self.df[self.bool_obj_indexer]

def time_boolean_rows_boolean(self):
self.df[self.boolean_indexer]


class DataFrameNumericIndexing:
def setup(self):
Expand Down
3 changes: 3 additions & 0 deletions doc/source/reference/extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ objects.
api.extensions.register_series_accessor
api.extensions.register_index_accessor
api.extensions.ExtensionDtype
api.extensions.is_bool_indexer
api.extensions.check_bool_array_indexer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a real reason to expose check_book_array_indexer now?
is_bool_indexer also shouldn't be in api.extension, api.types would be ok, though it was never meant to be public.

can we defer both of these as exposing to the public until we actually see use?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to defer exposing those utilities. Will push an update this evening.


.. autosummary::
:toctree: api/
Expand All @@ -26,6 +28,7 @@ objects.
api.extensions.ExtensionArray
arrays.PandasArray


.. We need this autosummary so that methods and attributes are generated.
.. Separate block, since they aren't classes.

Expand Down
23 changes: 23 additions & 0 deletions doc/source/user_guide/boolean.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ Nullable Boolean Data Type

.. versionadded:: 1.0.0


.. _boolean.indexing:

Indexing with NA values
-----------------------

pandas does not allow indexing with NA values. Attempting to do so
will raise a ``ValueError``.

.. ipython:: python
:okexcept:

s = pd.Series([1, 2, 3])
mask = pd.array([True, False, pd.NA], dtype="boolean")
s[mask]

The missing values will need to be explicitly filled with True or False prior
to using the array as a mask.

.. ipython:: python

s[mask.fillna(False)]

.. _boolean.kleene:

Kleene Logical Operations
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ Datetimelike
- Bug in :func:`pandas._config.localization.get_locales` where the ``locales -a`` encodes the locales list as windows-1252 (:issue:`23638`, :issue:`24760`, :issue:`27368`)
- Bug in :meth:`Series.var` failing to raise ``TypeError`` when called with ``timedelta64[ns]`` dtype (:issue:`28289`)
- Bug in :meth:`DatetimeIndex.strftime` and :meth:`Series.dt.strftime` where ``NaT`` was converted to the string ``'NaT'`` instead of ``np.nan`` (:issue:`29578`)
- Bug in masking datetime-like arrays with a boolean mask of an incorrect length not raising an ``IndexError`` (:issue:`30308`)
- Bug in :attr:`Timestamp.resolution` being a property instead of a class attribute (:issue:`29910`)
- Bug in :func:`pandas.to_datetime` when called with ``None`` raising ``TypeError`` instead of returning ``NaT`` (:issue:`30011`)
- Bug in :func:`pandas.to_datetime` failing for `deques` when using ``cache=True`` (the default) (:issue:`29403`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/api/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
)
from pandas.core.algorithms import take # noqa: F401
from pandas.core.arrays import ExtensionArray, ExtensionScalarOpsMixin # noqa: F401
from pandas.core.common import is_bool_indexer # noqa: F401
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel ought to move is_bool_indexer to a more usual location, pandas.core.dtypes.common is better

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think core.indexers

from pandas.core.indexing import check_bool_array_indexer # noqa: F401
21 changes: 18 additions & 3 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from pandas.core import nanops, ops
from pandas.core.algorithms import take
from pandas.core.arrays import ExtensionArray, ExtensionOpsMixin
from pandas.core.common import is_bool_indexer

if TYPE_CHECKING:
from pandas._typing import Scalar
Expand Down Expand Up @@ -307,11 +308,25 @@ 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..
Copy link
Contributor

Choose a reason for hiding this comment

The 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):
# import here to avoid circular import. Probably need to restructure
from pandas.core.indexing import check_bool_array_indexer

if is_integer(item):
if self._mask[item]:
return self.dtype.na_value
return self._data[item]

elif 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):
Expand All @@ -329,7 +344,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(
Expand Down Expand Up @@ -503,7 +518,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)
Expand All @@ -515,7 +530,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)
Expand Down
13 changes: 9 additions & 4 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1990,16 +1990,21 @@ def __getitem__(self, key):
"""
Return an item.
"""
from pandas.core.indexing import check_bool_array_indexer

if isinstance(key, (int, np.integer)):
i = self._codes[key]
if i == -1:
return np.nan
else:
return self.categories[i]
else:
return self._constructor(
values=self._codes[key], dtype=self.dtype, fastpath=True
)

elif com.is_bool_indexer(key):
key = check_bool_array_indexer(self, key)

return self._constructor(
values=self._codes[key], dtype=self.dtype, fastpath=True
)

def __setitem__(self, key, value):
"""
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ def __getitem__(self, key):
return self._box_func(val)

if com.is_bool_indexer(key):
key = np.asarray(key, dtype=bool)
from pandas.core.indexing import check_bool_array_indexer

key = check_bool_array_indexer(self, key)
if key.all():
key = slice(0, None, None)
else:
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from pandas.core import nanops, ops
from pandas.core.algorithms import take
from pandas.core.arrays import ExtensionArray, ExtensionOpsMixin
from pandas.core.common import is_bool_indexer
from pandas.core.ops import invalid_comparison
from pandas.core.ops.common import unpack_zerodim_and_defer
from pandas.core.tools.numeric import to_numeric
Expand Down Expand Up @@ -367,10 +368,17 @@ def fmt(x):
return fmt

def __getitem__(self, item):
# Importing this at the top-level causes many unrelated(?) mypy failures
from pandas.core.indexing import check_bool_array_indexer

if is_integer(item):
if self._mask[item]:
return self.dtype.na_value
return self._data[item]

elif 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):
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pandas import compat
from pandas.core import nanops
from pandas.core.algorithms import searchsorted, take, unique
from pandas.core.common import is_bool_indexer
from pandas.core.construction import extract_array
from pandas.core.missing import backfill_1d, pad_1d

Expand Down Expand Up @@ -231,9 +232,15 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
# Pandas ExtensionArray Interface

def __getitem__(self, item):
# Avoid mypy failures when importing at the top-level
from pandas.core.indexing import check_bool_array_indexer

if isinstance(item, type(self)):
item = item._ndarray

elif is_bool_indexer(item):
item = check_bool_array_indexer(self, item)

result = self._ndarray[item]
if not lib.is_scalar(item):
result = type(self)(result)
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The 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)
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ def is_bool_indexer(key: Any) -> bool:
Returns
-------
bool
Whether `key` is a valid boolean indexer.

Raises
------
ValueError
When the array is an object-dtype ndarray or ExtensionArray
and contains missing values.

See Also
--------
api.extensions.check_bool_array_indexer : Check that `key`
is a valid mask for an array, and convert to an ndarary.
"""
na_msg = "cannot index with vector containing NA / NaN values"
if isinstance(key, (ABCSeries, np.ndarray, ABCIndex)) or (
Expand Down
72 changes: 65 additions & 7 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pandas.core.dtypes.generic import ABCDataFrame, ABCMultiIndex, ABCSeries
from pandas.core.dtypes.missing import _infer_fill_value, isna

from pandas._typing import AnyArrayLike
import pandas.core.common as com
from pandas.core.indexers import is_list_like_indexer, length_of_indexer
from pandas.core.indexes.api import Index, InvalidIndexError
Expand Down Expand Up @@ -2268,6 +2269,69 @@ def convert_to_index_sliceable(obj, key):
return None


def check_bool_array_indexer(array: AnyArrayLike, mask: AnyArrayLike) -> np.ndarray:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would like to see these utilities split out of here (pandas.core.indexing is huge). but can certainly be done later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also simple enough it could go into core.indexers

"""
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.

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
"""
result = np.asarray(mask, dtype=bool)
# GH26658
if len(result) != len(array):
raise IndexError(f"Item wrong length {len(result)} instead of {len(array)}.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we worry about shape here? i.e. either handle or disallow ndim>1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. This is just an extraction from is_bool_indexer, so on master we expect shape to be handled elsewhere. It's not clear to me that we'd want to always disallow ndim > 1

return result


def check_bool_indexer(index: Index, key) -> np.ndarray:
"""
Check if key is a valid boolean indexer for an object with such index and
Expand Down Expand Up @@ -2308,13 +2372,7 @@ def check_bool_indexer(index: Index, key) -> np.ndarray:
else:
if is_sparse(result):
result = result.to_dense()
result = np.asarray(result, dtype=bool)

# GH26658
if len(result) != len(index):
raise IndexError(
f"Item wrong length {len(result)} instead of {len(index)}."
)
result = check_bool_array_indexer(index, result)

return result

Expand Down
Loading