We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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.
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())
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.
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
This is broken in 1.4.1 because the index names get lost.
For 1.3.5:
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:
The text was updated successfully, but these errors were encountered:
It looks like this has nothing to do with read_csv actually. loc removes the index names, hence they are missing afterwards
Sorry, something went wrong.
code sample for pandas-dev#46317
be300e8
xref #45621, #45623, #43507
Successfully merging a pull request may close this issue.
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
Issue Description
The example code above gives different outputs on Pandas 1.3.5 and 1.4.1.
On 1.3.5 I get
On 1.4.1 I get
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
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:
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
The text was updated successfully, but these errors were encountered: