UnicodeEncodeError with to_sql and unicode column names · Issue #11431 · pandas-dev/pandas (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

@jorisvandenbossche

Description

@jorisvandenbossche

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).