ascending/descending on multiple index levels with sortlevel
· Issue #10905 · pandas-dev/pandas (original) (raw)
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).