BUG: Empty string multilevel column names

This issue has been created since 2022-11-18.

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

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-12-07
Star Count 36164
Watcher Count 1118
Fork Count 15472
Issue Count 3683

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Different implementations for increment (`i+=1` and `i++`) 2 2022-05-13 2022-12-01
gimp-image-resize does nothing and without error 4 2017-07-08 2022-09-17
Error: spawn nmcli ENOENT (Linux/Raspberry Pi) 15 2021-08-17 2022-10-11
eddy throw exception 2 2022-02-27 2022-11-20
subscriptions module use-case 2 2022-06-25 2022-09-22
Display trait implementation for time::Time incorrectly trims the zeros from the sub-second part 1 2022-09-21 2022-11-28
Discussion: CSS strategy for halogen applications 17 2022-10-16 2022-11-30
Communication with JS loaded via CDN 2 2022-05-24 2022-11-21
Thoughts on rpc pattern? 8 2020-10-28 2022-11-19
Contribution contact update 1 2021-09-12 2022-12-05
Gamepad sensitivity increases by using FusionFix 2 2020-02-24 2022-10-26
Compiling on MinGW (Windows 10) - Incompatible Pointer error 1 2021-09-25 2022-08-17
Align UI breakpoints w/ design file 0 2021-11-09 2022-10-27
Link Thread instead of "thread number" 0 2022-05-08 2022-10-01
Prevent escape key from closing application 5 2022-10-24 2022-11-26
How to reach this code? 1 2022-10-25 2022-11-26
Resolver.resolve_filename() missing 0 2021-08-10 2022-11-30
Draggable not working with React useState and useEffect hooks 5 2022-05-19 2022-11-08
Make NpgsqlParameter lookup case-sensitive and optimize parameter lookup 1 2021-10-08 2022-09-16
Typing of useRuntimeConfig.public 0 2022-04-23 2022-11-10
Failed to use horovod for multi-gpu training 2 2022-05-07 2022-11-18
verdaccio resolves versions of unplubished versions of npm.js packages 3 2022-09-21 2022-11-23
[FEATURE REQUEST] Analogous Functionality for `SELECT * FROM table` in `SQLTable` 8 2021-10-29 2022-11-21
Deleting Backup not deleting the backups 3 2021-07-01 2022-10-05
Weapon part hides and doesn't show up after switching weapons. 1 2021-04-06 2022-11-13
[kube-prometheus-stack] support a way to add pod labels onto node-exporter metrics 2 2021-10-20 2022-11-20
the default `config.toml` doesn't contain the right structure for `allowed_redirect_urls` 1 2022-09-05 2022-10-10
Pin tensorflow version for tensorflow-with-gpu-preview 0 2019-12-11 2022-09-30
WebTorrent when downloading sometimes, torrent stops downloading, but it shows like it still is with 1000s of mb/s which is not possible or true. 1 2022-08-10 2022-11-27
[installer] Disable definitely-gp by default 0 2022-03-11 2022-09-25
why not check whether p.works has available worker before you spawn a new worker? 0 2022-11-10 2022-11-28
Packet Injection Functions page is incompelte and doesn't include FwpsInjectMacSendAsync/FwpsInjectMacReceiveAsync. 2 2022-02-16 2022-12-05
[🐛 Bug]: Selenium Hub container - status code is 200, even if the grid isn't ready 8 2022-02-23 2022-10-09
Section "Deploy the container app" should be consolidated or omitted. 5 2022-05-28 2022-11-10
[Issue]: CBR files don't always play within Jellyfin 2 2022-06-13 2022-07-08
[Issue]: Email Notifications 9.0.0.0 IP address issues 1 2022-06-13 2022-10-09
[Issue]: VAAPI H.264 transcode fails 1 2022-06-13 2022-10-20
Fix ImageFromReader(), doesn't render text in from file.svg 1 2021-11-02 2022-10-24
Have VPN icon display connection status indicator on taskbar (Windows) 1 2022-04-07 2022-11-14
[mlir][tensor] tensor.cast + tensor.insert_slice canonicalization generates invalid IR 7 2022-01-09 2022-10-11
ecos-python: license does not appear to be Apache 2.0? 2 2022-05-09 2022-10-24
Recordings are not shown if the first record of the day is broken. 9 2021-09-28 2022-10-31
Extensions can't be installed from local files 1 2021-05-01 2022-11-23
Drop "tls" option for pyMongo 0 2022-07-18 2022-11-08
QUESTION: need to get file content with API 5 2021-10-10 2022-10-01
Implement/refactor GA tracking events for admin bar menu 4 2021-09-15 2022-10-24
Not able to load .glb file in habitat_sim.Simulator(). 2 2022-01-19 2022-11-28
Why lamportsPerSignature * 100 ? 2 2022-02-16 2022-12-05
[Bug] api.logger.profile没有实现 0 2022-09-22 2022-10-14
Prepare rollouts for week of 2022-05-30 0 2022-05-30 2022-11-22