Skip to content

Commit 25f402c

Browse files
Backport PR #42469: REGR: indexing with list subclass (#42489)
Co-authored-by: jbrockmendel <[email protected]>
1 parent 3bcbe9b commit 25f402c

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

doc/source/whatsnew/v1.3.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Fixed regressions
1818
- :class:`DataFrame` constructed with with an older version of pandas could not be unpickled (:issue:`42345`)
1919
- Performance regression in constructing a :class:`DataFrame` from a dictionary of dictionaries (:issue:`42338`)
2020
- Fixed regression in :meth:`DataFrame.agg` dropping values when the DataFrame had an Extension Array dtype, a duplicate index, and ``axis=1`` (:issue:`42380`)
21+
- Fixed regression in indexing with a ``list`` subclass incorrectly raising ``TypeError`` (:issue:`42433`, :issue:42461`)
2122
-
2223

2324
.. ---------------------------------------------------------------------------

pandas/core/common.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ def is_bool_indexer(key: Any) -> bool:
143143
return True
144144
elif isinstance(key, list):
145145
# check if np.array(key).dtype would be bool
146-
return len(key) > 0 and lib.is_bool_list(key)
146+
if len(key) > 0:
147+
if type(key) is not list:
148+
# GH#42461 cython will raise TypeError if we pass a subclass
149+
key = list(key)
150+
return lib.is_bool_list(key)
147151

148152
return False
149153

pandas/tests/test_common.py

+25
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,28 @@ def test_non_bool_array_with_na(self):
165165
# in particular, this should not raise
166166
arr = np.array(["A", "B", np.nan], dtype=object)
167167
assert not com.is_bool_indexer(arr)
168+
169+
def test_list_subclass(self):
170+
# GH#42433
171+
172+
class MyList(list):
173+
pass
174+
175+
val = MyList(["a"])
176+
177+
assert not com.is_bool_indexer(val)
178+
179+
val = MyList([True])
180+
assert com.is_bool_indexer(val)
181+
182+
def test_frozenlist(self):
183+
# GH#42461
184+
data = {"col1": [1, 2], "col2": [3, 4]}
185+
df = pd.DataFrame(data=data)
186+
187+
frozen = df.index.names[1:]
188+
assert not com.is_bool_indexer(frozen)
189+
190+
result = df[frozen]
191+
expected = df[[]]
192+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)