Skip to content

BUG: indexing with boolean-like Index, #11119 #11178

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1941,8 +1941,8 @@ def _getitem_array(self, key):
warnings.warn("Boolean Series key will be reindexed to match "
"DataFrame index.", UserWarning)
elif len(key) != len(self.index):
raise ValueError('Item wrong length %d instead of %d.' %
(len(key), len(self.index)))
indexer = self.ix._convert_to_indexer(key, axis=1)
return self.take(indexer, axis=1, convert=True)
# check_bool_indexer will throw exception if Series key cannot
# be reindexed to match DataFrame rows
key = check_bool_indexer(self.index, key)
Expand Down
76 changes: 41 additions & 35 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,50 +1077,56 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):
return labels.get_locs(obj)
elif is_list_like_indexer(obj):
if is_bool_indexer(obj):
obj = check_bool_indexer(labels, obj)
inds, = obj.nonzero()
return inds
else:
if isinstance(obj, Index):
objarr = obj.values
if is_bool_indexer(list(labels)):
pass
elif len(obj) == self.obj.shape[axis]:
obj = check_bool_indexer(labels, obj)
inds, = obj.nonzero()
return inds
else:
objarr = _asarray_tuplesafe(obj)
raise ValueError('Item wrong length %d instead of %d.' %
(len(obj), len(self.obj.index)))

if isinstance(obj, Index):
objarr = obj.values
else:
objarr = _asarray_tuplesafe(obj)

# The index may want to handle a list indexer differently
# by returning an indexer or raising
indexer = labels._convert_list_indexer(objarr, kind=self.name)
if indexer is not None:
return indexer
# The index may want to handle a list indexer differently
# by returning an indexer or raising
indexer = labels._convert_list_indexer(objarr, kind=self.name)
if indexer is not None:
return indexer

# this is not the most robust, but...
if (isinstance(labels, MultiIndex) and
not isinstance(objarr[0], tuple)):
level = 0
_, indexer = labels.reindex(objarr, level=level)
# this is not the most robust, but...
if (isinstance(labels, MultiIndex) and
not isinstance(objarr[0], tuple)):
level = 0
_, indexer = labels.reindex(objarr, level=level)

# take all
if indexer is None:
indexer = np.arange(len(labels))
# take all
if indexer is None:
indexer = np.arange(len(labels))

check = labels.levels[0].get_indexer(objarr)
else:
level = None
check = labels.levels[0].get_indexer(objarr)
else:
level = None

# unique index
if labels.is_unique:
indexer = check = labels.get_indexer(objarr)
# unique index
if labels.is_unique:
indexer = check = labels.get_indexer(objarr)

# non-unique (dups)
else:
(indexer,
missing) = labels.get_indexer_non_unique(objarr)
check = indexer
# non-unique (dups)
else:
(indexer,
missing) = labels.get_indexer_non_unique(objarr)
check = indexer

mask = check == -1
if mask.any():
raise KeyError('%s not in index' % objarr[mask])
mask = check == -1
if mask.any():
raise KeyError('%s not in index' % objarr[mask])

return _values_from_object(indexer)
return _values_from_object(indexer)

else:
try:
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,12 @@ def test_booleanindex(self):
subIndex = self.strIndex[list(boolIdx)]
for i, val in enumerate(subIndex):
self.assertEqual(subIndex.get_loc(val), i)

def test_booleanLabel(self):
df = pd.DataFrame({True: [1,2,5,9],
False: [6,1,13,8]})
self.assert_frame_equal(df[df[True]>3], df[2:])
self.assert_frame_equal(df[[False,True]], df)

def test_fancy(self):
sl = self.strIndex[[1, 2, 3]]
Expand Down