Skip to content

Commit

Permalink
BUG: can pass level names to swaplevel, GH #379
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Nov 22, 2011
1 parent 0b31dfb commit 3eca074
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ pandas 0.5.1
- Handle min_periods=0 case in moving window functions (GH #365)
- Fixed corner cases in DataFrame.apply/pivot with empty DataFrame (GH #378)
- Fixed repr exception when Series name is a tuple
- Always return DateRange from asfreq (GH #390)
-

Thanks
------
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from datetime import time
from itertools import izip
import gc

import numpy as np

Expand Down Expand Up @@ -1197,6 +1196,9 @@ def swaplevel(self, i, j):
new_labels = list(self.labels)
new_names = list(self.names)

i = self._get_level_number(i)
j = self._get_level_number(j)

new_levels[i], new_levels[j] = new_levels[j], new_levels[i]
new_labels[i], new_labels[j] = new_labels[j], new_labels[i]
new_names[i], new_names[j] = new_names[j], new_names[i]
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,19 @@ def test_join(self):

def test_swaplevel(self):
swapped = self.frame['A'].swaplevel(0, 1)
swapped2 = self.frame['A'].swaplevel('first', 'second')
self.assert_(not swapped.index.equals(self.frame.index))
assert_series_equal(swapped, swapped2)

back = swapped.swaplevel(0, 1)
back2 = swapped.swaplevel('second', 'first')
self.assert_(back.index.equals(self.frame.index))
assert_series_equal(back, back2)

ft = self.frame.T
swapped = ft.swaplevel('first', 'second', axis=1)
exp = self.frame.swaplevel('first', 'second').T
assert_frame_equal(swapped, exp)

def test_swaplevel_panel(self):
panel = Panel({'ItemA' : self.frame,
Expand All @@ -507,6 +516,7 @@ def test_swaplevel_panel(self):
result = panel.swaplevel(0, 1, axis='major')
expected = panel.copy()
expected.major_axis = expected.major_axis.swaplevel(0, 1)
tm.assert_panel_equal(result, expected)

def test_insert_index(self):
df = self.ymd[:5].T
Expand Down

0 comments on commit 3eca074

Please sign in to comment.