Skip to content

Commit a89216a

Browse files
author
Matias Heikkilä
committed
TST: Test sorting levels not aligned with index (#25775)
1 parent 6d2398a commit a89216a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pandas/tests/frame/test_sorting.py

+25
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pandas.tests.frame.common import TestData
1212
import pandas.util.testing as tm
1313
from pandas.util.testing import assert_frame_equal, assert_series_equal
14+
from io import StringIO
1415

1516

1617
class TestDataFrameSorting(TestData):
@@ -227,6 +228,30 @@ def test_stable_descending_multicolumn_sort(self):
227228
kind='mergesort')
228229
assert_frame_equal(sorted_df, expected)
229230

231+
def test_sort_multi_index(self):
232+
# GH 25775
233+
s = ',a,b,c,d,e' + \
234+
'0,0.548,7,0.602,0.544,0.423' + \
235+
'1,0.645,4,0.891,0.963,0.383' + \
236+
'2,0.791,5,0.568,0.925,0.071' + \
237+
'3,0.087,0,0.832,0.778,0.870' + \
238+
'4,0.978,7,0.461,0.780,0.118' + \
239+
'5,0.639,1,0.944,0.521,0.414' + \
240+
'6,0.264,7,0.456,0.568,0.018' + \
241+
'7,0.617,6,0.616,0.943,0.681' + \
242+
'8,0.359,4,0.697,0.060,0.666' + \
243+
'9,0.670,2,0.128,0.315,0.363'
244+
data = StringIO(s)
245+
df1 = (pd.read_csv(data)
246+
.assign(b=lambda df: (df.b * 10).astype(int)))
247+
df2 = df1.set_index(['a', 'b', 'c']).sort_index(
248+
axis=0, level=['b', 'a'])
249+
expected = DataFrame(df2.index.to_frame()[['a', 'b']].values,
250+
columns=['a', 'b']).assign(
251+
b=lambda df: (df.b).astype(int))
252+
result = df1.sort_values(by=['b', 'a']).reset_index()[['a', 'b']]
253+
tm.assert_frame_equal(result, expected)
254+
230255
def test_stable_categorial(self):
231256
# GH 16793
232257
df = DataFrame({

0 commit comments

Comments
 (0)