From f5e672934a55fe833252fae34b008084922d12c6 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 9 Jul 2021 12:12:43 -0700 Subject: [PATCH] REGR: indexing with list subclass --- doc/source/whatsnew/v1.3.1.rst | 1 + pandas/core/common.py | 6 +++++- pandas/tests/test_common.py | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.1.rst b/doc/source/whatsnew/v1.3.1.rst index 255747c3c5c6d..2c615618a98ef 100644 --- a/doc/source/whatsnew/v1.3.1.rst +++ b/doc/source/whatsnew/v1.3.1.rst @@ -18,6 +18,7 @@ Fixed regressions - :class:`DataFrame` constructed with with an older version of pandas could not be unpickled (:issue:`42345`) - Performance regression in constructing a :class:`DataFrame` from a dictionary of dictionaries (:issue:`42338`) - Fixed regression in :meth:`DataFrame.agg` dropping values when the DataFrame had an Extension Array dtype, a duplicate index, and ``axis=1`` (:issue:`42380`) +- Fixed regression in indexing with a ``list`` subclass incorrectly raising ``TypeError`` (:issue:`42433`, :issue:42461`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/common.py b/pandas/core/common.py index 37674cdb34aec..b32614577393d 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -145,7 +145,11 @@ def is_bool_indexer(key: Any) -> bool: return True elif isinstance(key, list): # check if np.array(key).dtype would be bool - return len(key) > 0 and lib.is_bool_list(key) + if len(key) > 0: + if type(key) is not list: + # GH#42461 cython will raise TypeError if we pass a subclass + key = list(key) + return lib.is_bool_list(key) return False diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index a9b2176fba19a..5e9a53f32e0b7 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -162,3 +162,28 @@ def test_non_bool_array_with_na(self): # in particular, this should not raise arr = np.array(["A", "B", np.nan], dtype=object) assert not com.is_bool_indexer(arr) + + def test_list_subclass(self): + # GH#42433 + + class MyList(list): + pass + + val = MyList(["a"]) + + assert not com.is_bool_indexer(val) + + val = MyList([True]) + assert com.is_bool_indexer(val) + + def test_frozenlist(self): + # GH#42461 + data = {"col1": [1, 2], "col2": [3, 4]} + df = pd.DataFrame(data=data) + + frozen = df.index.names[1:] + assert not com.is_bool_indexer(frozen) + + result = df[frozen] + expected = df[[]] + tm.assert_frame_equal(result, expected)