DOC/TST: Series.reorder_levels() can take names; added tests
State: closed

Minor change to the docstring. It said you must refer to the position (not keys). Keys work:

``` python
In [18]: res.head()
Out[18]: 
same_employer  stamp     
-3             1996-02-01        2
-2             1996-01-01        1
               1996-02-01        1
               1996-03-01        4
 1             1996-01-01    13877
dtype: int64

In [19]: res.reorder_levels(['stamp', 'same_employer']).head()
Out[19]: 
stamp       same_employer
1996-02-01  -3                   2
1996-01-01  -2                   1
1996-02-01  -2                   1
1996-03-01  -2                   4
1996-01-01   1               13877
dtype: int64
```


Labels:

Comments:
Are there any tests for reorder_levels, I can't see any in test_series ? We should test if adding to docstring. :s

Would you mind adding some tests to confirm this behaviour to this PR?

Is it the same for a DataFrame? Then the docstring there could be changed also.

maybe repush this? Looks like a Travis hiccup

I'll look at those two points and repush later tonight/tomorrow.

On Nov 22, 2013, at 5:39 PM, "Jeff Tratner" <notifications@github.com<mailto:notifications@github.com>> wrote:

maybe repush this? Looks like a Travis hiccup

—
Reply to this email directly or view it on GitHubhttps://github.com/pydata/pandas/pull/5573#issuecomment-29118285.

I'm not seeing any tests for this in series/frame/generic. I'll write a set.

Also, does this seems odd to anyone else, given the name?

``` python
In [40]: s.reorder_levels([2, 2, 2])
Out[40]: 
0  0  0    0
1  1  1    1
0  0  0    2
1  1  1    3
0  0  0    4
1  1  1    5
dtype: int64
```

I would have expected it to check that it's an actual reordering.

Hmm, this also isn't great:

``` python
In [66]: s
Out[66]: 
L1   L2     L2   # Note the non unique index names
bar  one    0     0
     two    1     1
     three  0     2
     one    1     3
     two    0     4
     three  1     5
dtype: int64


In [68]: s.reorder_levels(['L2', 'L1', 'L2'])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-68-3ed0635cff91> in <module>()
----> 1 s.reorder_levels(['L2', 'L1', 'L2'])

/Users/tom/Envs/pandas-dev/lib/python2.7/site-packages/pandas-0.12.0_993_gda89834-py2.7-macosx-10.8-x86_64.egg/pandas/core/series.pyc in reorder_levels(self, order)
   1846 
   1847         result = self.copy()
-> 1848         result.index = result.index.reorder_levels(order)
   1849         return result
   1850 

/Users/tom/Envs/pandas-dev/lib/python2.7/site-packages/pandas-0.12.0_993_gda89834-py2.7-macosx-10.8-x86_64.egg/pandas/core/index.pyc in reorder_levels(self, order)
   2698         ----------
   2699         """
-> 2700         order = [self._get_level_number(i) for i in order]
   2701         if len(order) != self.nlevels:
   2702             raise AssertionError(('Length of order must be same as '

/Users/tom/Envs/pandas-dev/lib/python2.7/site-packages/pandas-0.12.0_993_gda89834-py2.7-macosx-10.8-x86_64.egg/pandas/core/index.pyc in _get_level_number(self, level)
   2185         except ValueError:
   2186             if not isinstance(level, int):
-> 2187                 raise KeyError('Level %s not found' % str(level))
   2188             elif level < 0:
   2189                 level += self.nlevels

KeyError: 'Level L2 not found'
```

Raising an error is the right behavior, but the message could be better.  Separate PR though!

Thanks for looking into this, do you want us to merge (before sep PR) ?

I said nothing about _me_ doing a separate PR :)  I won't be able to get to it today.

I think this ones good to go.

@TomAugspurger dang, too slow, looks like it needs a rebase :(

I'll try later tonight hopefully. Before the rc maintenance branch is opened at least.

On Nov 23, 2013, at 10:23 PM, "Andy Hayden" <notifications@github.com<mailto:notifications@github.com>> wrote:

@TomAugspurgerhttps://github.com/TomAugspurger dang, too slow, looks like it needs a rebase :(

—
Reply to this email directly or view it on GitHubhttps://github.com/pydata/pandas/pull/5573#issuecomment-29148647.

rebased and repushed. The conflict was just on the docstring.