Skip to content

Commit

Permalink
BUG: Regression in .loc accepting a boolean Index as an indexer (pand…
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback authored and alanbato committed Nov 10, 2017
1 parent 7d9e789 commit e512d8b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ Indexing
- Bug in ``IntervalIndex`` where performing a scalar lookup fails for included right endpoints of non-overlapping monotonic decreasing indexes (:issue:`16417`, :issue:`17271`)
- Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` when no valid entry (:issue:`17400`)
- Bug in :func:`Series.rename` when called with a `callable`, incorrectly alters the name of the `Series`, rather than the name of the `Index`. (:issue:`17407`)
- Regression in ``.loc`` accepting a boolean ``Index`` as an indexer (:issue:`17131`)

I/O
^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pandas import compat
from pandas.compat import long, zip, iteritems
from pandas.core.config import get_option
from pandas.core.dtypes.generic import ABCSeries
from pandas.core.dtypes.generic import ABCSeries, ABCIndex
from pandas.core.dtypes.common import _NS_DTYPE
from pandas.core.dtypes.inference import _iterable_not_string
from pandas.core.dtypes.missing import isna, isnull, notnull # noqa
Expand Down Expand Up @@ -182,7 +182,7 @@ def _maybe_box_datetimelike(value):


def is_bool_indexer(key):
if isinstance(key, (ABCSeries, np.ndarray)):
if isinstance(key, (ABCSeries, np.ndarray, ABCIndex)):
if key.dtype == np.object_:
key = np.asarray(_values_from_object(key))

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,23 @@ def test_loc_getitem_label_slice(self):
self.check_result('mixed slice', 'loc', slice(2, 4, 2), 'ix', slice(
2, 4, 2), typs=['mixed'], axes=0, fails=TypeError)

def test_loc_index(self):
# gh-17131
# a boolean index should index like a boolean numpy array

df = DataFrame(
np.random.random(size=(5, 10)),
index=["alpha_0", "alpha_1", "alpha_2", "beta_0", "beta_1"])

mask = df.index.map(lambda x: "alpha" in x)
expected = df.loc[np.array(mask)]

result = df.loc[mask]
tm.assert_frame_equal(result, expected)

result = df.loc[mask.values]
tm.assert_frame_equal(result, expected)

def test_loc_general(self):

df = DataFrame(
Expand Down

0 comments on commit e512d8b

Please sign in to comment.