Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
tiezhishizi opened this issue Sep 28, 2021 · 2 comments
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).

Comments

@tiezhishizi
Copy link

  • 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

@tiezhishizi tiezhishizi added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 28, 2021
@mroeschke mroeschke added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 2, 2021
@Svanazar
Copy link
Contributor

Svanazar commented Oct 8, 2021

take

Svanazar added a commit to Svanazar/pandas that referenced this issue Oct 9, 2021
…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.
@jreback jreback added this to the 1.4 milestone Oct 10, 2021
@Svanazar Svanazar removed their assignment Nov 19, 2021
@simonjayhawkins
Copy link
Member

removing milestone

@simonjayhawkins simonjayhawkins removed this from the 1.4 milestone Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants