Skip to content
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

ascending/descending on multiple index levels with sortlevel #10905

Closed
roblevy opened this issue Aug 26, 2015 · 2 comments
Closed

ascending/descending on multiple index levels with sortlevel #10905

roblevy opened this issue Aug 26, 2015 · 2 comments
Labels
API Design MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@roblevy
Copy link
Contributor

roblevy commented Aug 26, 2015

In #928, support for list of boolean/binary values was added to control ascending/descending when sorting on multiple columns.

This doesn't work with sortlevel, but nor does it raise an error. This seems potentially "dangerous" to me.

In [23]: levels = list('ABC')

In [24]: length = 10

In [25]: df = pd.DataFrame(dict(alpha=np.random.choice(levels, length),
   ....:     num=np.random.random(length), vals=np.random.random(length)))

In [26]: df = df.set_index(['alpha', 'num'])

In [27]: df
Out[27]: 
                    vals
alpha num               
C     0.101632  0.566144
      0.334399  0.242917
A     0.741676  0.807468
C     0.406477  0.756441
A     0.225341  0.214034
C     0.623214  0.696784
A     0.576516  0.181786
      0.509289  0.144184
B     0.510504  0.155965
A     0.093283  0.356627

In [28]: # Sort reverse alphabetically. But then `num` is backwards!

In [29]: df.sortlevel(['alpha', 'num'], ascending=False)
Out[29]: 
                    vals
alpha num               
C     0.623214  0.696784
      0.406477  0.756441
      0.334399  0.242917
      0.101632  0.566144
B     0.510504  0.155965
A     0.741676  0.807468
      0.576516  0.181786
      0.509289  0.144184
      0.225341  0.214034
      0.093283  0.356627

In [30]: # Attempt to sort reverse alphabetically with `num` sorting forwards:

In [31]: df.sortlevel(['alpha', 'num'], ascending=[False, True])
Out[31]: 
                    vals
alpha num               
A     0.093283  0.356627
      0.225341  0.214034
      0.509289  0.144184
      0.576516  0.181786
      0.741676  0.807468
B     0.510504  0.155965
C     0.101632  0.566144
      0.334399  0.242917
      0.406477  0.756441
      0.623214  0.696784

As you can see, this last sortlevel does not behave as expected. I'd expect to see alpha sorted descending (Z - A) and num sorted ascending (0 - 1).

@jreback
Copy link
Contributor

jreback commented Aug 26, 2015

This works in master with the new sort framework. .sortlevel I don't think was passing things thru correctly.

In [15]: np.random.seed(1234)

In [19]: df = pd.DataFrame(dict(alpha=np.random.choice(levels, length),
   ....:     num=np.random.random(length), vals=np.random.random(length))).set_index(['alpha','num'])

In [20]: df
Out[20]: 
                    vals
alpha num               
C     0.704261  0.594625
B     0.704581  0.533310
C     0.218792  0.043324
A     0.924868  0.561433
C     0.442141  0.329668
      0.909316  0.502967
A     0.059809  0.111894
B     0.184287  0.607194
      0.047355  0.565945
A     0.674881  0.006764

In [21]: df.sortlevel(level=['alpha','num'],ascending=[False,True])
Out[21]: 
                    vals
alpha num               
C     0.218792  0.043324
      0.442141  0.329668
      0.704261  0.594625
      0.909316  0.502967
B     0.047355  0.565945
      0.184287  0.607194
      0.704581  0.533310
A     0.059809  0.111894
      0.674881  0.006764
      0.924868  0.561433

@jreback jreback closed this as completed Aug 26, 2015
@jreback
Copy link
Contributor

jreback commented Aug 26, 2015

xref #9816

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants