Skip to content

to_csv and read_csv return inconsistent result when value contains \r #27737

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
KaitoHH opened this issue Aug 4, 2019 · 3 comments
Closed

to_csv and read_csv return inconsistent result when value contains \r #27737

KaitoHH opened this issue Aug 4, 2019 · 3 comments
Labels
IO CSV read_csv, to_csv

Comments

@KaitoHH
Copy link

KaitoHH commented Aug 4, 2019

Code Sample, a copy-pastable example if possible

dff = pd.DataFrame(data={'a':['a1\r','a2'],'b':['b1','b2']})
print("===BEFORE SAVE===")
print(dff)
dff.to_csv('test.csv', index=False)
print("===AFTER SAVE===")
print(pd.read_csv('test.csv'))

The result will be

===BEFORE SAVE===
      a   b
0  a1\r  b1
1    a2  b2
===AFTER SAVE===
     a    b
0   a1  NaN
1  NaN   b1
2   a2   b2

Problem description

The problem can be resolved by adding quoting=csv.QUOTE_NONNUMERIC option, but it seems like a bug under default parameters of to_csv.

Expected Output

===BEFORE SAVE===
      a   b
0  a1\r  b1
1    a2  b2
===AFTER SAVE===
      a   b
0  a1\r  b1
1    a2  b2

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-151-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: 4.6.3
pip: 19.1.1
setuptools: 41.0.1
Cython: 0.29.13
numpy: 1.16.4
scipy: 1.3.0
pyarrow: None
xarray: None
IPython: 7.5.0
sphinx: 2.1.2
patsy: None
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.1.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: 4.8.0
html5lib: None
sqlalchemy: 1.3.5
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
@Haabibi
Copy link

Haabibi commented Aug 16, 2019

This was also bothersome for me. I will check if this issue can be solved by simply putting quoting=csv.QUOTE_NONNUMERIC as default.

@jbrockmendel jbrockmendel added the IO CSV read_csv, to_csv label Oct 16, 2019
@asishm
Copy link
Contributor

asishm commented Oct 9, 2020

dupe of #10018

@phofl
Copy link
Member

phofl commented Dec 13, 2020

Closing as duplicate

@phofl phofl closed this as completed Dec 13, 2020
@phofl phofl added this to the No action milestone Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO CSV read_csv, to_csv
Projects
None yet
Development

No branches or pull requests

5 participants