Skip to content

UnicodeEncodeError with to_sql and unicode column names #11431

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
jorisvandenbossche opened this issue Oct 26, 2015 · 1 comment
Closed

UnicodeEncodeError with to_sql and unicode column names #11431

jorisvandenbossche opened this issue Oct 26, 2015 · 1 comment
Labels
Bug IO SQL to_sql, read_sql, read_sql_query Unicode Unicode strings
Milestone

Comments

@jorisvandenbossche
Copy link
Member

From SO: http://stackoverflow.com/questions/33337798/unicodeencodeerror-when-using-pandas-method-to-sql-on-a-dataframe-with-unicode-c

To reproduce:

from sqlalchemy import create_engine 
engine = create_engine('sqlite:///:memory:')
df = pd.DataFrame([[1,2],[3,4]], columns = [u'\xe9',u'b'])
df.to_sql('data', engine, if_exists='replace', index=False)

which gives UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128), because of this line: https://github.com/pydata/pandas/blob/master/pandas/io/sql.py#L856, where it stringifies the individual columns names with str (which fails on python 2 with unicode).

@jorisvandenbossche jorisvandenbossche added Bug Unicode Unicode strings IO SQL to_sql, read_sql, read_sql_query labels Oct 26, 2015
@jorisvandenbossche jorisvandenbossche added this to the 0.17.1 milestone Oct 26, 2015
@jorisvandenbossche
Copy link
Member Author

I suppose we should just use compat.text_type instead of str

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO SQL to_sql, read_sql, read_sql_query Unicode Unicode strings
Projects
None yet
Development

No branches or pull requests

2 participants