Skip to content

BUG: DataFrame construction with columns argument set to a MultiIndex creates an empty DataFrame. #39904

Closed
@DriesSchaumont

Description

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

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

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


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

>>> import pandas as pd
>>> from numpy import nan
>>>
>>>
>>> column_index = pd.MultiIndex.from_product([["sample1", "sample2"], [True], [True]],
...                                           names=["Name", "Sample_column", "Write"])
>>> table = pd.DataFrame(data={"sample1": [100.0, nan, 100.0, 10.0],
...                            "sample2": [80.0, 20.0, 100.0, 100.0]})
>>> table.columns = column_index
>>>
>>> table2 = pd.DataFrame(data={"sample1": [100.0, nan, 100.0, 10.0],
...                             "sample2": [80.0, 20.0, 100.0, 100.0]},
...                             columns=column_index)
>>> table
Name          sample1 sample2
Sample_column    True    True
Write            True    True
0               100.0    80.0
1                 NaN    20.0
2               100.0   100.0
3                10.0   100.0
>>> table2
Empty DataFrame
Columns: [(sample1, True, True), (sample2, True, True)]
Index:

Problem description

If a MultiIndex is used to set the columns using the columns argument from the constructor, the MultiIndex is set correctly but the whole dataframe becomes empty. If the columns index is applied manually after construction by using df.columns, the dataframe does not lose its data. I think this is either a bug, or a clear warning or error message should be given why the dataframe becomes empty.

Expected Output

I expect the constructor to return a non-empty dataframe with the MultiIndex applied to the columns. The API reference mentions that is can be an 'Index or array-like', but I don't know if a MultiIndex is allowed.

Output of pd.show_versions()

pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-58-generic
Version : #64-Ubuntu SMP Wed Dec 9 08:16:25 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.2
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 44.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions