Skip to content

DataFrame.to_csv with empty dataframe probleme quoting #15891

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
parisni opened this issue Apr 4, 2017 · 10 comments · Fixed by #41607
Closed

DataFrame.to_csv with empty dataframe probleme quoting #15891

parisni opened this issue Apr 4, 2017 · 10 comments · Fixed by #41607
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@parisni
Copy link

parisni commented Apr 4, 2017

Code Sample, a copy-pastable example if possible

>>>import pandas as pd
>>> pd.DataFrame([""]).to_csv("/tmp/test.csv", header= False, index= False, quoting=3)
[...]Error: single empty field record must be quoted

Problem description

I cannot get an empty csv. There is allways quote for the first row

Expected Output

I should get a csv with an empty row.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.0.final.0 python-bits: 64 OS: Linux OS-release: 4.8.0-45-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: fr_FR.UTF-8 LOCALE: fr_FR.UTF-8

pandas: 0.19.2
nose: 1.3.7
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.0
scipy: 0.18.1
statsmodels: 0.6.1
xarray: None
IPython: 5.1.0
sphinx: 1.5.2
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: 1.2.0
tables: 3.3.0
numexpr: 2.6.1
matplotlib: 2.0.0
openpyxl: 2.4.1
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.2
bs4: 4.5.3
html5lib: None
httplib2: 0.10.3
apiclient: 1.6.2
sqlalchemy: 1.1.5
pymysql: None
psycopg2: None
jinja2: 2.9.4
boto: 2.45.0
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Apr 4, 2017

Is this meant to be something?

In [3]: pd.DataFrame([""])
Out[3]: 
  0
0  
In [2]: pd.DataFrame([]).to_csv(None, header= False, index= False, quoting=3)
Out[2]: ''
In [6]: pd.DataFrame(['foo']).to_csv(None, header= False, index= False, quoting=2)
Out[6]: '"foo"\n'

@jreback
Copy link
Contributor

jreback commented Apr 4, 2017

cc @gfyoung

@jreback
Copy link
Contributor

jreback commented Apr 4, 2017

this actually looks ok to me.

@jreback jreback added the IO CSV read_csv, to_csv label Apr 4, 2017
@parisni
Copy link
Author

parisni commented Apr 4, 2017

How should I get an empy csv with 2 rows ?
from [None, None]

cf http://stackoverflow.com/questions/20665141/writing-a-pandas-dataframe-into-a-csv-file-with-some-empty-rows

@parisni
Copy link
Author

parisni commented Apr 4, 2017

pd.DataFrame([None, None]).to_csv(None, header= False, index= False)
'""\n\n'

@jreback
Copy link
Contributor

jreback commented Apr 4, 2017

In [4]: pd.DataFrame(index=[0,1]).to_csv(None, header= False, index= False)
Out[4]: '\n\n'

None is an actual value.

@parisni
Copy link
Author

parisni commented Apr 4, 2017

Thank, this way solves my problem.
However, I cannot anderstand what the logic behind.
pd.DataFrame([None, None]).to_csv(None, header= False, index= False)
should return
'""\n""\n'
then

@jreback
Copy link
Contributor

jreback commented Apr 4, 2017

so the missing values are getting filled by the na_rep. But they are still quoted, which seems a bit odd. (as when I put an actual value they are NOT quoted).

In [6]:  pd.DataFrame([None, None])
Out[6]: 
      0
0  None
1  None

In [7]: pd.DataFrame([None, None]).to_csv(None, header= False, index= False, na_rep='foo')
Out[7]: 'foo\nfoo\n'

In [8]: pd.DataFrame([None, None]).to_csv(None, header= False, index= False, na_rep='')
Out[8]: '""\n\n'

@jreback jreback added Bug Difficulty Intermediate Output-Formatting __repr__ of pandas objects, to_string labels Apr 4, 2017
@jreback jreback added this to the Next Major Release milestone Apr 4, 2017
@jreback
Copy link
Contributor

jreback commented Apr 4, 2017

I think this is a bug, so marked. @parisni welcome to have a PR to fix! (code is actually not bad for writing csvs....)

@mroeschke
Copy link
Member

This looks fixed on master. Could use a test

In [3]: In [8]: pd.DataFrame([None, None]).to_csv(None, header= False, index= False, na_rep='')
Out[3]: '""\n""\n'

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug IO CSV read_csv, to_csv Output-Formatting __repr__ of pandas objects, to_string labels May 8, 2021
@mroeschke mroeschke mentioned this issue May 21, 2021
10 tasks
@jreback jreback modified the milestones: Contributions Welcome, 1.3 May 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants