Skip to content

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

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

Closed
2 of 3 tasks
torfsen opened this issue Mar 11, 2022 · 2 comments · Fixed by #46322
Closed
2 of 3 tasks

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

torfsen opened this issue Mar 11, 2022 · 2 comments · Fixed by #46322
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Milestone

Comments

@torfsen
Copy link

torfsen commented Mar 11, 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

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

@torfsen torfsen added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 11, 2022
@phofl
Copy link
Member

phofl commented Mar 11, 2022

It looks like this has nothing to do with read_csv actually. loc removes the index names, hence they are missing afterwards

@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 11, 2022
@jreback jreback modified the milestones: 1.5, 1.4.2 Mar 11, 2022
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Mar 12, 2022
@simonjayhawkins
Copy link
Member

xref #45621, #45623, #43507

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants