Skip to content

Commit f72aa71

Browse files
committed
BUG: unstack on unicode name level breaks GH9856
[PYCON 2015 Sprints]
1 parent 9d9fa24 commit f72aa71

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

doc/source/whatsnew/v0.16.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,5 @@ Bug Fixes
132132
- Bug in unequal comparisons between a ``Series`` of dtype `"category"` and a scalar (e.g. ``Series(Categorical(list("abc"), categories=list("cba"), ordered=True)) > "b"``, which wouldn't use the order of the categories but use the lexicographical order. (:issue:`9848`)
133133

134134
- Bug in unequal comparisons between categorical data and a scalar, which was not in the categories (e.g. ``Series(Categorical(list("abc"), ordered=True)) > "d"``. This returned ``False`` for all elements, but now raises a ``TypeError``. Equality comparisons also now return ``False`` for ``==`` and ``True`` for ``!=``. (:issue:`9848`)
135+
136+
- Bug in ``MultiIndex.sortlevel()`` results in unicode level name breaks (:issue:`9875`)

pandas/core/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4023,7 +4023,7 @@ def sortlevel(self, level=0, ascending=True, sort_remaining=True):
40234023
labels = list(self.labels)
40244024
shape = list(self.levshape)
40254025

4026-
if isinstance(level, (str, int)):
4026+
if isinstance(level, (compat.string_types, int)):
40274027
level = [level]
40284028
level = [self._get_level_number(lev) for lev in level]
40294029

pandas/tests/test_frame.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from pandas.compat import(
1818
map, zip, range, long, lrange, lmap, lzip,
19-
OrderedDict, u, StringIO
19+
OrderedDict, u, StringIO, string_types
2020
)
2121
from pandas import compat
2222

@@ -12428,6 +12428,30 @@ def test_unstack_bool(self):
1242812428
['c', 'l']]))
1242912429
assert_frame_equal(rs, xp)
1243012430

12431+
def test_unstack_level_binding(self):
12432+
# GH9856
12433+
mi = pd.MultiIndex(
12434+
levels=[[u'foo', u'bar'], [u'one', u'two'], [u'a', u'b']],
12435+
labels=[[0, 0, 1, 1], [0, 1, 0, 1], [1, 0, 1, 0]],
12436+
names=[u'first', u'second', u'third'])
12437+
s = pd.Series(0, index=mi)
12438+
result = s.unstack([1, 2]).stack(0)
12439+
12440+
expected_mi = pd.MultiIndex(
12441+
levels=[['foo', 'bar'], ['one', 'two']],
12442+
labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
12443+
names=['first', 'second'])
12444+
12445+
expected = pd.DataFrame(np.array([[np.nan, 0],
12446+
[0, np.nan],
12447+
[np.nan, 0],
12448+
[0, np.nan]],
12449+
dtype=np.float64),
12450+
index=expected_mi,
12451+
columns=pd.Index(['a', 'b'], name='third'))
12452+
12453+
self.assert_frame_equal(result, expected)
12454+
1243112455
def test_unstack_to_series(self):
1243212456
# check reversibility
1243312457
data = self.frame.unstack()

0 commit comments

Comments
 (0)