Skip to content

BUG: Difference in CSV roundtrip between 1.3.5 and 1.4.1 #46317

Closed
@torfsen

Description

@torfsen

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

from io import StringIO
import pandas as pd

print(pd.__version__)
print("----------")

csv_file = StringIO("a,b,c,x")
csv_file.seek(0)

df = pd.read_csv(
    csv_file,
    index_col=["a", "b", "c"],
)

df.loc[(1,2,3)] = "foo"
print(df.to_csv())

Issue Description

The example code above gives different outputs on Pandas 1.3.5 and 1.4.1.

On 1.3.5 I get

1.3.5
----------
a,b,c,x
1,2,3,foo

On 1.4.1 I get

1.4.1
----------
,,,x
1,2,3,foo

Note that for 1.4.1 the CSV columns corresponding to DataFrame index columns have no header.

Expected Behavior

To me, the 1.3.5 behavior looks correct.

My use case is as follows: I have an CSV file that is initially empty, aside from the header line. I then have code that

  1. Reads the CSV file
  2. Updates the data
  3. Re-writes the CSV file

This is broken in 1.4.1 because the index names get lost.

Installed Versions

For 1.3.5:

INSTALLED VERSIONS

commit : 66e3805
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-100-generic
Version : #113-Ubuntu SMP Thu Feb 3 18:43:29 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.22.2
pytz : 2019.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 52.0.0.post20210125
Cython : None
pytest : 7.0.1
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.3 (dt dec pq3 ext lo64)
jinja2 : 2.11.1
IPython : 8.0.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.5.1
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 7.0.0
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.4.31
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
numba : None

For 1.4.1:

INSTALLED VERSIONS ------------------ commit : 66e3805 python : 3.8.5.final.0 python-bits : 64 OS : Linux OS-release : 5.4.0-100-generic Version : #113-Ubuntu SMP Thu Feb 3 18:43:29 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.22.2
pytz : 2019.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 52.0.0.post20210125
Cython : None
pytest : 7.0.1
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.3 (dt dec pq3 ext lo64)
jinja2 : 2.11.1
IPython : 8.0.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.5.1
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 7.0.0
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.4.31
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndex

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions