Skip to content

loc allows different length boolean indexer #26658

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

Closed
TomAugspurger opened this issue Jun 5, 2019 · 0 comments · Fixed by #26911
Closed

loc allows different length boolean indexer #26658

TomAugspurger opened this issue Jun 5, 2019 · 0 comments · Fixed by #26911
Labels
Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@TomAugspurger
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant