Skip to content

DOC/TST: Series.reorder_levels() can take names; added tests #5573

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
Nov 24, 2013

Conversation

TomAugspurger
Copy link
Contributor

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

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

@hayd
Copy link
Contributor

hayd commented Nov 22, 2013

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?

@jorisvandenbossche
Copy link
Member

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

@jtratner
Copy link
Contributor

maybe repush this? Looks like a Travis hiccup

@TomAugspurger
Copy link
Contributor Author

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

On Nov 22, 2013, at 5:39 PM, "Jeff Tratner" <[email protected]mailto:[email protected]> wrote:

maybe repush this? Looks like a Travis hiccup


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

@TomAugspurger
Copy link
Contributor Author

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?

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.

@TomAugspurger
Copy link
Contributor Author

Hmm, this also isn't great:

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!

@hayd
Copy link
Contributor

hayd commented Nov 23, 2013

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

@TomAugspurger
Copy link
Contributor Author

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.

@hayd
Copy link
Contributor

hayd commented Nov 24, 2013

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

@TomAugspurger
Copy link
Contributor Author

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" <[email protected]mailto:[email protected]> 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//pull/5573#issuecomment-29148647.

TST: reorder was untested

DOC/TST reorder_levels takes names
@TomAugspurger
Copy link
Contributor Author

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

hayd added a commit that referenced this pull request Nov 24, 2013
DOC/TST: Series.reorder_levels() can take names; added tests
@hayd hayd merged commit 476b6e3 into pandas-dev:master Nov 24, 2013
@TomAugspurger TomAugspurger deleted the reorder_docstring branch November 3, 2016 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants