Skip to content

BUG: GH4516 Fixed issue with sorting a duplicate multi-index that has multiple dtypes #4522

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

Merged
merged 1 commit into from
Aug 9, 2013
Merged
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 @@ -135,6 +135,7 @@ pandas 0.13
- Raise on set indexing with a Panel and a Panel as a value which needs alignment (:issue:`3777`)
- frozenset objects now raise in the ``Series`` constructor (:issue:`4482`,
:issue:`4480`)
- Fixed issue with sorting a duplicate multi-index that has multiple dtypes (:issue:`4516`)

pandas 0.12
===========
Expand Down
9 changes: 6 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3304,10 +3304,13 @@ def sortlevel(self, level=0, axis=0, ascending=True, inplace=False):
new_axis, indexer = the_axis.sortlevel(level, ascending=ascending)

if self._is_mixed_type and not inplace:
if axis == 0:
return self.reindex(index=new_axis)
ax = 'index' if axis == 0 else 'columns'

if new_axis.is_unique:
d = { ax : new_axis }
else:
return self.reindex(columns=new_axis)
d = { ax : indexer, 'takeable' : True }
return self.reindex(**d)

if inplace:
if axis == 1:
Expand Down
13 changes: 11 additions & 2 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,8 +2165,17 @@ def reindex(self, target, method=None, level=None, limit=None,
if self.equals(target):
indexer = None
else:
indexer = self.get_indexer(target, method=method,
limit=limit)
if self.is_unique:
indexer = self.get_indexer(target, method=method,
limit=limit)
else:
if takeable:
if method is not None or limit is not None:
raise ValueError("cannot do a takeable reindex with "
"with a method or limit")
return self[target], target

raise Exception("cannot handle a non-takeable non-unique multi-index!")

if not isinstance(target, MultiIndex):
if indexer is None:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,18 @@ def test_duplicate_groupby_issues(self):
result = s.groupby(s.index).first()
self.assertEquals(len(result), 3)

def test_duplicate_mi(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback longshot: do you recall if, deep down, this test is for sortlevel (not sort_index(level)) or for df.loc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was for loc iirc

# GH 4516
df = DataFrame([['foo','bar',1.0,1],['foo','bar',2.0,2],['bah','bam',3.0,3],
['bah','bam',4.0,4],['foo','bar',5.0,5],['bah','bam',6.0,6]],
columns=list('ABCD'))
df = df.set_index(['A','B'])
df = df.sortlevel(0)
result = df.loc[('foo','bar')]
expected = DataFrame([['foo','bar',1.0,1],['foo','bar',2.0,2],['foo','bar',5.0,5]],
columns=list('ABCD')).set_index(['A','B'])
assert_frame_equal(result,expected)

def test_multiindex_set_index(self):
# segfault in #3308
d = {'t1': [2, 2.5, 3], 't2': [4, 5, 6]}
Expand Down