Skip to content

BUG: Empty string multilevel column names #49765

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

Open
3 tasks done
prhbrt opened this issue Nov 18, 2022 · 3 comments
Open
3 tasks done

BUG: Empty string multilevel column names #49765

prhbrt opened this issue Nov 18, 2022 · 3 comments
Labels
Docs Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@prhbrt
Copy link

prhbrt commented Nov 18, 2022

Pandas version checks

  • 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 main branch of pandas.

Reproducible Example

Empty string multilevel column names that follow an indexed column are removed from results.

    import pandas as pd
    
    data = pd.DataFrame({('b', '', '', '', 'a', ''): []})
    
    print(data.columns)
    print(data['b'].columns)

results in this:

    MultiIndex([('b', '', '', '', 'a', '')], )
    MultiIndex([('a', '')], )

Issue Description

Missing column levels after indexing. This seems to be explicitly programmed behavior, that recursively eats empty top column names, but I can't find anything about this in the user guide on indexing, nor why empty column names should be treated differently.

Expected Behavior

MultiIndex([('', '', '', 'a', '')], ) was expected for the second output.

Installed Versions

This issue reproduces on:

Google colab:

------------------ commit : 66e3805 python : 3.7.15.final.0 python-bits : 64 OS : Linux OS-release : 5.10.133+ Version : #1 SMP Fri Aug 26 08:44:51 UTC 2022 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.3.5
numpy : 1.21.6
pytz : 2022.6
dateutil : 2.8.2
pip : 21.1.3
setuptools : 57.4.0
Cython : 0.29.32
pytest : 3.6.4
hypothesis : None
sphinx : 1.8.6
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.9.5 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.9.0
pandas_datareader: 0.9.0
bs4 : 4.6.3
bottleneck : None
fsspec : 2022.10.0
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.8.4
odfpy : None
openpyxl : 3.0.10
pandas_gbq : 0.17.9
pyarrow : 6.0.1
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : 1.4.43
tables : 3.7.0
tabulate : 0.8.10
xarray : 0.20.2
xlrd : 1.1.0
xlwt : 1.3.0
numba : 0.56.4

Main-branch (2.0.0.dev0+697.g8020bf1b2)

INSTALLED VERSIONS ------------------ commit : 91111fd python : 3.10.6.final.0 python-bits : 64 OS : Linux OS-release : 5.15.0-53-generic Version : #59-Ubuntu SMP Mon Oct 17 18:53:30 UTC 2022 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.5.1
numpy : 1.23.0
pytz : 2022.1
dateutil : 2.8.2
setuptools : 59.6.0
pip : 22.0.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : 2.9.1
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.5
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None
tzdata : None

1.5.1:

INSTALLED VERSIONS ------------------ commit : 91111fd python : 3.10.6.final.0 python-bits : 64 OS : Linux OS-release : 5.15.0-53-generic Version : #59-Ubuntu SMP Mon Oct 17 18:53:30 UTC 2022 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.5.1
numpy : 1.23.0
pytz : 2022.1
dateutil : 2.8.2
setuptools : 59.6.0
pip : 22.0.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : 2.9.1
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.5
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None
tzdata : None

@prhbrt prhbrt added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 18, 2022
@phofl phofl added the Indexing Related to indexing on series/frames, not to indexes themselves label Dec 5, 2022
@phofl
Copy link
Member

phofl commented Dec 5, 2022

We have a couple of tests that are testing exactly this behavior. So don't think this should be considered a bug. (test_frame_mixed_depth_get)

This is from way back and has been around since forever (#2596) one of the tests was added there

@phofl phofl added Docs and removed Bug labels Dec 5, 2022
@phofl
Copy link
Member

phofl commented Dec 5, 2022

We should maybe document this?

@prhbrt
Copy link
Author

prhbrt commented Jan 10, 2023

We should maybe document this?

I think that's important, this behavior causes obscure KeyErrors when not considered.

I'm also interested in the reasoning behind this explicitly implemented and tested behavior.

@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

No branches or pull requests

4 participants