Closed
Description
Code Sample, a copy-pastable example if possible
This throws UnicodeDecodeError in python2
pd.pivot_table(pd.DataFrame({'foo': [1,2,3]}), index=['foo'], aggfunc=len, margins=True, margins_name=u'Δοκιμή')
Expected Output
Not throwing exception!
output of pd.show_versions()
INSTALLED VERSIONS ------------------ commit: None python: 2.7.11.final.0 python-bits: 32 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel byteorder: little LC_ALL: None LANG: None pandas: 0.18.1 nose: None pip: 8.1.2 setuptools: 20.10.1 Cython: None numpy: 1.11.0 scipy: None statsmodels: None xarray: None IPython: None sphinx: None patsy: None dateutil: 2.5.2 pytz: 2016.4 blosc: None bottleneck: None tables: None numexpr: None matplotlib: None openpyxl: None xlrd: 0.9.4 xlwt: 1.0.0 xlsxwriter: None lxml: None bs4: None html5lib: None httplib2: None apiclient: None sqlalchemy: None pymysql: None psycopg2: 2.6.1 (dt dec pq3 ext) jinja2: None boto: None pandas_datareader: None
The above is a well-known problem when mixing unicode and non-unicode strings in python2: The fix (for python 2) is very simple, just add a u
in front of the exception_msg
string literal in https://github.com/pydata/pandas/blob/master/pandas/tools/pivot.py#L178 i.e line 178 should be exception_msg = u'Conflicting name "{0}" in margins'.format(margins_name)
I am not sure what's the correct way to fix that in python 3 and a python2-3 compatible way so I won't provide a PR for that.