pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.81k stars 17.98k forks source link

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

Open tiezhishizi opened 3 years ago

tiezhishizi commented 3 years ago

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 : 5f648bf1706dd75a9ca0d29f26eadfbb595fe52b 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
Svanazar commented 3 years ago

take

simonjayhawkins commented 2 years ago

removing milestone