Skip to content

Fix for GH 6885 - get_dummies chokes on unicode values #6975

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
wants to merge 4 commits into from
Closed
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 @@ -381,6 +381,7 @@ Bug Fixes
- Bug in arithmetic operations affecting to NaT (:issue:`6873`)
- Bug in ``Series.str.extract`` where the resulting ``Series`` from a single
group match wasn't renamed to the group name
- Bug causing UnicodeEncodeError when get_dummies called with unicode values and a prefix (:issue:`6885`)

pandas 0.13.1
-------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ def get_dummies(data, prefix=None, prefix_sep='_', dummy_na=False):
dummy_mat[cat.labels == -1] = 0

if prefix is not None:
dummy_cols = ['%s%s%s' % (prefix, prefix_sep, str(v))
dummy_cols = ['%s%s%s' % (prefix, prefix_sep, v)
for v in levels]
else:
dummy_cols = levels
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pandas.core.reshape import (melt, convert_dummies, lreshape, get_dummies,
wide_to_long)
import pandas.util.testing as tm
from pandas.compat import StringIO, cPickle, range
from pandas.compat import StringIO, cPickle, range, u

_multiprocess_can_split_ = True

Expand Down Expand Up @@ -199,6 +199,16 @@ def test_include_na(self):
exp_just_na = DataFrame(Series(1.0,index=[0]),columns=[nan])
assert_array_equal(res_just_na.values, exp_just_na.values)

def test_unicode(self): # See GH 6885 - get_dummies chokes on unicode values
import unicodedata
e = 'e'
eacute = unicodedata.lookup('LATIN SMALL LETTER E WITH ACUTE')
s = [e, eacute, eacute]
res = get_dummies(s, prefix='letter')
exp = DataFrame({'letter_e': {0: 1.0, 1: 0.0, 2: 0.0},
u('letter_%s') % eacute: {0: 0.0, 1: 1.0, 2: 1.0}})
assert_frame_equal(res, exp)

class TestConvertDummies(tm.TestCase):
def test_convert_dummies(self):
df = DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
Expand Down