You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
The text was updated successfully, but these errors were encountered:
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
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.As you can see, this last
sortlevel
does not behave as expected. I'd expect to seealpha
sorted descending (Z - A) andnum
sorted ascending (0 - 1).The text was updated successfully, but these errors were encountered: