Skip to content

to_csv chokes if not passed sep as a string, even when encoding is set to unicode #6035

@maxgrenderjones

Description

@maxgrenderjones

I am using anaconda python 2.7, pandas 0.12.0, with from __future__ import unicode_literals

The bug

When I call dataframe.to_csv(fname, sep=sep, index=index, float_format=float_format, **csvkwargs) and pass in sep=',', pandas throws an exception:

 File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 1410, in to_csv formatter.save() File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 959, in save quoting=self.quoting) File "C:\Anaconda\lib\site-packages\pandas\core\common.py", line 1770, in __init__ self.writer = csv.writer(self.queue, dialect=dialect, **kwds) TypeError: "delimiter" must be an 1-character string 

The workaround


If I change this to:
dataframe.to_csv(fname, sep=str(sep), index=index, float_format=float_format, **csvkwargs) everything works as you might expect. (I have observerved this error both when I pass in no encoding and when I pass in encoding='Windows 1252')

What I would expect


When passed unicode, pandas should:

  • If encoding is set, and the unicode sep is one character long i.e. len(sep)==1 (is it really necessary to have this one character restriction?), use the sep character passed in (encoded appropriately)
  • If encoding is not set, then check that the default encoding (I don't know whether this is ascii or platform dependent) of sep is only one character long

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions