Skip to content

BUG: Fix crash using to_csv with QUOTE_NONE #4752

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

Merged
merged 1 commit into from
Sep 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
- Fix some inconsistencies with ``Index.rename`` and ``MultiIndex.rename``,
etc. (:issue:`4718`, :issue:`4628`)
- Bug in using ``iloc/loc`` with a cross-sectional and duplicate indicies (:issue:`4726`)
- Bug with using ``QUOTE_NONE`` with ``to_csv`` causing ``Exception``. (:issue:`4328`)

pandas 0.12
===========
Expand Down
18 changes: 12 additions & 6 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ def __init__(self, obj, path_or_buf, sep=",", na_rep='', float_format=None,
cols=None, header=True, index=True, index_label=None,
mode='w', nanRep=None, encoding=None, quoting=None,
line_terminator='\n', chunksize=None, engine=None,
tupleize_cols=True):
tupleize_cols=True, quotechar='"'):

self.engine = engine # remove for 0.13
self.obj = obj
Expand All @@ -807,6 +807,11 @@ def __init__(self, obj, path_or_buf, sep=",", na_rep='', float_format=None,
quoting = csv.QUOTE_MINIMAL
self.quoting = quoting

if quoting == csv.QUOTE_NONE:
# prevents crash in _csv
quotechar = None
self.quotechar = quotechar

self.line_terminator = line_terminator

#GH3457
Expand Down Expand Up @@ -950,13 +955,14 @@ def save(self):
close = True

try:
writer_kwargs = dict(lineterminator=self.line_terminator,
delimiter=self.sep, quoting=self.quoting,
quotechar=self.quotechar)
if self.encoding is not None:
self.writer = com.UnicodeWriter(f, lineterminator=self.line_terminator,
delimiter=self.sep, encoding=self.encoding,
quoting=self.quoting)
writer_kwargs['encoding'] = self.encoding
self.writer = com.UnicodeWriter(f, **writer_kwargs)
else:
self.writer = csv.writer(f, lineterminator=self.line_terminator,
delimiter=self.sep, quoting=self.quoting)
self.writer = csv.writer(f, **writer_kwargs)

if self.engine == 'python':
# to be removed in 0.13
Expand Down
16 changes: 12 additions & 4 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import datetime, timedelta, time
import operator
import re
import csv
import unittest
import nose

Expand Down Expand Up @@ -5465,8 +5466,6 @@ def test_to_csv_float_format(self):
assert_frame_equal(rs, xp)

def test_to_csv_quoting(self):
import csv

df = DataFrame({'A': [1, 2, 3], 'B': ['foo', 'bar', 'baz']})

buf = StringIO()
Expand All @@ -5489,8 +5488,6 @@ def test_to_csv_quoting(self):
self.assertEqual(buf.getvalue(), text)

def test_to_csv_unicodewriter_quoting(self):
import csv

df = DataFrame({'A': [1, 2, 3], 'B': ['foo', 'bar', 'baz']})

buf = StringIO()
Expand All @@ -5505,6 +5502,17 @@ def test_to_csv_unicodewriter_quoting(self):

self.assertEqual(result, expected)

def test_to_csv_quote_none(self):
# GH4328
df = DataFrame({'A': ['hello', '{"hello"}']})
for encoding in (None, 'utf-8'):
buf = StringIO()
df.to_csv(buf, quoting=csv.QUOTE_NONE,
encoding=encoding, index=False)
result = buf.getvalue()
expected = 'A\nhello\n{"hello"}\n'
self.assertEqual(result, expected)

def test_to_csv_index_no_leading_comma(self):
df = DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]},
index=['one', 'two', 'three'])
Expand Down