Skip to content

BUG: read_csv inconsistent behavior #57792

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
3 tasks done
fgr1986 opened this issue Mar 9, 2024 · 7 comments · Fixed by #57874
Closed
3 tasks done

BUG: read_csv inconsistent behavior #57792

fgr1986 opened this issue Mar 9, 2024 · 7 comments · Fixed by #57874
Labels

Comments

@fgr1986
Copy link

fgr1986 commented Mar 9, 2024

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
from io import StringIO

csv_data = """0,1.992422433360555e-06,0,1.992422433374335e-06,0,0,0,0.01867750880504259,0,0.8,0,0.002722413402762714,0,0.002722413278797551
3e-12,1.992418292306889e-06,3e-12,1.992418292319521e-06,3e-12,0,3e-12,0.01867750887016915,3e-12,0.8,3e-12,0.002722413686257528,3e-12,0.002722413562292257
9.000000000000001e-12,1.992416874989637e-06,9.000000000000001e-12,1.992416875007223e-06,9.000000000000001e-12,0,9.000000000000001e-12,0.01867750900743637,9.000000000000001e-12,0.8,9.000000000000001e-12,0.00272241426436429,9.000000000000001e-12,0.002722414140398993"""

# Use StringIO to create a file-like object from the string
csv_data_io1 = StringIO(csv_data)
csv_data_io2 = StringIO(csv_data)

# Use pandas.read_csv to read from the file-like object
df1 = pd.read_csv(csv_data_io1)
df2 = pd.read_csv(csv_data_io2, header=None)

# assertions
print(df1.columns.tolist())
print(df2.iloc[0].tolist())

Issue Description

Columns are incorrectly read from CSV

Expected Behavior

df1.columns.tolist() being the same as df2.iloc[0].tolist()
i.e.: [0.0, 1.992422433360555e-06, 0.0, 1.992422433374335e-06, 0.0, 0.0, 0.0, 0.0186775088050425, 0.0, 0.8, 0.0, 0.0027224134027627, 0.0, 0.0027224132787975]

However, it is:
['0', '1.992422433360555e-06', '0.1', '1.992422433374335e-06', '0.2', '0.3', '0.4', '0.01867750880504259', '0.5', '0.8', '0.6', '0.002722413402762714', '0.7', '0.002722413278797551']
which is not any row in the file

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.9.7.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.133.1-microsoft-standard-WSL2
Version : #1 SMP Thu Oct 5 21:02:42 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.1
numpy : 1.26.4
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
Cython : None
pytest : 7.4.1
hypothesis : None
sphinx : 5.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.15.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.0
numba : None
numexpr : 2.8.7
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.11.4
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@fgr1986 fgr1986 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 9, 2024
@wleong1
Copy link

wleong1 commented Mar 11, 2024

I believe in your df1, pandas has used the first line as the header. However, headers should be distinct and hence pandas has added the ".1", ".2", ".3", etc. to all the headers that are the same, i.e. 0, to differentiate them.

@rhshadrach
Copy link
Member

Correct @wleong1 - I think this behavior should be documented in the docstring of read_csv for the header argument.

@rhshadrach rhshadrach added Docs IO CSV read_csv, to_csv good first issue and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 13, 2024
@fgr1986
Copy link
Author

fgr1986 commented Mar 13, 2024

Noted, thanks!

@BaloiJhoiss
Copy link

Is anybody working on that? If Not, I could help here.

@Aftabby
Copy link

Aftabby commented Mar 17, 2024

Hi @rhshadrach

I noticed that this issue is currently unassigned, and I'm interested in working on it. Could you please assign it to me? I believe I can contribute to document in the docstring of read_csv for the header argument (as per you mentioned).

Thanks!

@Dxuian
Copy link

Dxuian commented Mar 21, 2024

is this still open for contribution , im new to open source please be kind ?

@KarthikNayak
Copy link

is this still open for contribution , im new to open source please be kind ?

@Dxuian if you noticed above your message, it says that a PR has already been created by @quangngd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants