Skip to content

Commit d46b027

Browse files
mpenkovjreback
authored andcommitted
Prevent UnicodeDecodeError in pivot_table under Py2 (#17489)
1 parent 9a84274 commit d46b027

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,7 @@ Reshaping
17051705
- Bug in ``pd.concat()`` in which concatenating with an empty dataframe with ``join='inner'`` was being improperly handled (:issue:`15328`)
17061706
- Bug with ``sort=True`` in ``DataFrame.join`` and ``pd.merge`` when joining on indexes (:issue:`15582`)
17071707
- Bug in ``DataFrame.nsmallest`` and ``DataFrame.nlargest`` where identical values resulted in duplicated rows (:issue:`15297`)
1708+
- Bug in :func:`pandas.pivot_table` incorrectly raising ``UnicodeError`` when passing unicode input for ```margins`` keyword (:issue:`13292`)
17081709

17091710
Numeric
17101711
^^^^^^^

pandas/core/reshape/pivot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def _add_margins(table, data, values, rows, cols, aggfunc,
145145
if not isinstance(margins_name, compat.string_types):
146146
raise ValueError('margins_name argument must be a string')
147147

148-
msg = 'Conflicting name "{name}" in margins'.format(name=margins_name)
148+
msg = u'Conflicting name "{name}" in margins'.format(name=margins_name)
149149
for level in table.index.names:
150150
if margins_name in table.index.get_level_values(level):
151151
raise ValueError(msg)

pandas/tests/reshape/test_pivot.py

+10
Original file line numberDiff line numberDiff line change
@@ -1625,3 +1625,13 @@ def test_isleapyear_deprecate(self):
16251625

16261626
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
16271627
assert isleapyear(2004)
1628+
1629+
def test_pivot_margins_name_unicode(self):
1630+
# issue #13292
1631+
greek = u'\u0394\u03bf\u03ba\u03b9\u03bc\u03ae'
1632+
frame = pd.DataFrame({'foo': [1, 2, 3]})
1633+
table = pd.pivot_table(frame, index=['foo'], aggfunc=len, margins=True,
1634+
margins_name=greek)
1635+
index = pd.Index([1, 2, 3, greek], dtype='object', name='foo')
1636+
expected = pd.DataFrame(index=index)
1637+
tm.assert_frame_equal(table, expected)

0 commit comments

Comments
 (0)