Skip to content

Raise a ValueError when index and data lengths don't match #26911

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 16 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ Indexing

- Improved exception message when calling :meth:`DataFrame.iloc` with a list of non-numeric objects (:issue:`25753`).
- Bug in :meth:`DataFrame.loc` and :meth:`Series.loc` where ``KeyError`` was not raised for a ``MultiIndex`` when the key was less than or equal to the number of levels in the :class:`MultiIndex` (:issue:`14885`).
- Bug in ``.iloc`` and ``.loc`` where ``ValueError`` was not raised for a boolean index with different length (:issue:`26658`).
- Bug in which :meth:`DataFrame.append` produced an erroneous warning indicating that a ``KeyError`` will be thrown in the future when the data to be appended contains new columns (:issue:`22252`).
- Bug in which :meth:`DataFrame.to_csv` caused a segfault for a reindexed data frame, when the indices were single-level :class:`MultiIndex` (:issue:`26303`).
- Fixed bug where assigning a :class:`arrays.PandasArray` to a :class:`pandas.core.frame.DataFrame` would raise error (:issue:`26390`)
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,9 @@ def _getbool_axis(self, key, axis=None):
axis = self.axis or 0
labels = self.obj._get_axis(axis)
key = check_bool_indexer(labels, key)
if 0 < len(labels) != len(key):
raise ValueError('Item wrong length %d instead of %d.' %
(len(key), len(labels)))
inds, = key.nonzero()
try:
return self.obj._take(inds, axis=axis)
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,19 @@ def test_iloc_getitem_bool(self):
typs=['labels', 'mixed', 'ts', 'floats', 'empty'],
fails=IndexError)

def test_iloc_getitem_bool_diff_len(self):
# GH26658
too_short = [True, False]
too_long = [True, False, True, False, False]
self.check_result('bool', 'iloc', too_short, 'ix', too_short,
typs=['ints', 'uints', 'labels',
'mixed', 'ts', 'floats'],
fails=ValueError)
self.check_result('bool', 'iloc', too_long, 'ix', too_long,
typs=['ints', 'uints', 'labels',
'mixed', 'ts', 'floats'],
fails=ValueError)

def test_iloc_getitem_slice(self):

# slices
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ def test_loc_getitem_bool(self):
self.check_result('bool', 'loc', b, 'ix', b, typs=['empty'],
fails=KeyError)

def test_loc_getitem_bool_diff_len(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as above

# GH26658
too_short = [True, False]
too_long = [True, False, True, False, False]
self.check_result('bool', 'loc', too_short, 'ix', too_short,
typs=['ints', 'uints', 'labels',
'mixed', 'ts', 'floats'],
fails=ValueError)
self.check_result('bool', 'loc', too_long, 'ix', too_long,
typs=['ints', 'uints', 'labels',
'mixed', 'ts', 'floats'],
fails=ValueError)

def test_loc_getitem_int_slice(self):

# ok
Expand Down