Skip to content

loc allows different length boolean indexer  #26658

@TomAugspurger

Description

@TomAugspurger

Is this a bug, or deliberate?

In [45]: a.loc[[True, False]] # too few? Out[45]: a 1 dtype: int64 In [46]: a.loc[[True, False, True, False, False]] # too many? Out[46]: a 1 c 3 dtype: int64

The fact that we raise here, when we have too many true values makes me think this is a bug

In [53]: a.loc[[True, False, True, False, True]] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) ~/Envs/dask-dev/lib/python3.7/site-packages/pandas/core/indexing.py in _getbool_axis(self, key, axis) 1517 try: -> 1518 return self.obj._take(inds, axis=axis) 1519 except Exception as detail: ~/Envs/dask-dev/lib/python3.7/site-packages/pandas/core/series.py in _take(self, indices, axis, is_copy) 3925 indices = ensure_platform_int(indices) -> 3926 new_index = self.index.take(indices) 3927 ~/Envs/dask-dev/lib/python3.7/site-packages/pandas/core/indexes/base.py in take(self, indices, axis, allow_fill, fill_value, **kwargs) 793 fill_value=fill_value, --> 794 na_value=self._na_value) 795 else: ~/Envs/dask-dev/lib/python3.7/site-packages/pandas/core/indexes/base.py in _assert_take_fillable(self, values, indices, allow_fill, fill_value, na_value) 819 else: --> 820 taken = values.take(indices) 821 return taken IndexError: index 4 is out of bounds for axis 0 with size 3 During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-53-9fd96bbd72ce> in <module> ----> 1 a.loc[[True, False, True, False, True]] ~/Envs/dask-dev/lib/python3.7/site-packages/pandas/core/indexing.py in __getitem__(self, key) 1498 1499 maybe_callable = com.apply_if_callable(key, self.obj) -> 1500 return self._getitem_axis(maybe_callable, axis=axis) 1501 1502 def _is_scalar_access(self, key): ~/Envs/dask-dev/lib/python3.7/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis) 1867 return self._get_slice_axis(key, axis=axis) 1868 elif com.is_bool_indexer(key): -> 1869 return self._getbool_axis(key, axis=axis) 1870 elif is_list_like_indexer(key): 1871 ~/Envs/dask-dev/lib/python3.7/site-packages/pandas/core/indexing.py in _getbool_axis(self, key, axis) 1518 return self.obj._take(inds, axis=axis) 1519 except Exception as detail: -> 1520 raise self._exception(detail) 1521 1522 def _get_slice_axis(self, slice_obj, axis=None): KeyError: IndexError('index 4 is out of bounds for axis 0 with size 3')

In the docs at http://pandas-docs.github.io/pandas-docs-travis/user_guide/indexing.html#boolean-indexing we say

You may select rows from a DataFrame using a boolean vector the same length as the DataFrame’s index

Note that this only affects .loc. Series.__getitem__ and DataFrame.__getitem__ are unaffected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions