Skip to content

Commit 919b6f7

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

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pandas/tests/frame/test_sorting.py

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

0 commit comments

Comments
 (0)