Skip to content

BUG: Selecting multiple columns from data frames with MultiIndex columns raises "ValueError" when each level of index is tuple  #43780

Open
@tiezhishizi

Description

@tiezhishizi

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
mi = pd.MultiIndex.from_tuples([(('a1','a2'),('b1','b2')), (('a1','a2'),('c1','c2'))])
df = pd.DataFrame(data=np.arange(2).reshape(1,2), columns=mi)

# This line works
df[(('a1','a2'),('b1','b2'))]
# This line would throw exception
df[[(('a1','a2'),('b1','b2'))]]

Issue Description

When a data frame has columns of MultiIndex, and each level is a tuple. Selecting columns with array of column labels would raise ValueError.

ValueError: Buffer has wrong number of dimensions

But selecting with a single label works.

Expected Behavior

Return columns with no error.

Installed Versions

INSTALLED VERSIONS

commit : 5f648bf
python : 3.8.11.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-1051-azure
Version : #53~18.04.1-Ubuntu SMP Fri Jun 18 22:32:58 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.2
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.0.1
setuptools : 52.0.0.post20210125
Cython : 0.29.24
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.26.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.1.3
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.4.23
tables : 3.6.1
tabulate : 0.8.9
xarray : None
xlrd : 2.0.1
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndexNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions