Skip to content

Commit 9a643e9

Browse files
authored
REGR: indexing with list subclass (#42469)
1 parent f329b24 commit 9a643e9

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
@@ -145,7 +145,11 @@ def is_bool_indexer(key: Any) -> bool:
145145
return True
146146
elif isinstance(key, list):
147147
# check if np.array(key).dtype would be bool
148-
return len(key) > 0 and lib.is_bool_list(key)
148+
if len(key) > 0:
149+
if type(key) is not list:
150+
# GH#42461 cython will raise TypeError if we pass a subclass
151+
key = list(key)
152+
return lib.is_bool_list(key)
149153

150154
return False
151155

pandas/tests/test_common.py

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

0 commit comments

Comments
 (0)