Skip to content

Commit d89f131

Browse files
committed
BUG: fix Frame getitem when column keys are nested tuples pandas-dev#43780
Multi-column selection when the column keys are nested tuples (for example when a MultiIndex has at each level a tuple) raised error due to a condition in asarray_tuplesafe. A key list of form [((1,))] converts to [[[1]]] through the call to np.array, resulting in ndim>2. However, only the case ndim=2 was further handled, causing a multi-dimensional array to be returned and subsequently raising error. This commit extends the ndim=2 condition for these type of keys.
1 parent 583eee5 commit d89f131

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

pandas/core/common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ def asarray_tuplesafe(values, dtype: NpDtype | None = None) -> np.ndarray:
240240
if issubclass(result.dtype.type, str):
241241
result = np.asarray(values, dtype=object)
242242

243-
if result.ndim == 2:
243+
# For ndim > 2 distinguish between nested tuples and multidimensional arrays
244+
if result.ndim == 2 or (result.ndim > 2 and isinstance(values[0][0], tuple)):
244245
# Avoid building an array of arrays:
245246
values = [tuple(x) for x in values]
246247
result = construct_1d_object_array_from_listlike(values)

0 commit comments

Comments
 (0)