Skip to content

BUG: read_csv: Columns Silently Forced into Multi Index  #60672

@cdelisle95

Description

@cdelisle95

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

import pandas as pd

data = """13.9,130,200,1000,2,1
12.1,120,200,1001,2,2
0.7,110,200,1000,2,3
""" 

df = pd.read_csv(
    pd.io.common.StringIO(data),
    header=None,
    names=["test1","test2","test3"],
    sep=",",
    encoding="utf-8-sig",
    index_col=None
)
print(df.shape)
print(df)

Issue Description

When calling pd.read_csv and using the arg 'names=...', if a user inputs fewer names than columns in the dataset, the names are applied to the final columns of the data and the first are silently placed into a multi index.

In my example, a clean csv with 6 columns is read using read_csv, where only 3 names are provided. The result is the final 3 columns (3,4,5) are named using the provided names and the first 3 are made into a multi index. No warning or error is raised.

Expected Behavior

My understanding is that the expected behavior is that the first columns will receive naming priority and the following ones will be named 'unnamed_column' or something along those lines.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.11.11
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.2.3
numpy : 2.2.1
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : None
IPython : 8.31.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.5
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : 0.23.0
tzdata : 2024.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions