@@ -230,6 +230,7 @@ def test_sort_index_intervalindex(self):
230
230
result = result .columns .levels [1 ].categories
231
231
tm .assert_index_equal (result , expected )
232
232
233
+ @pytest .mark .parametrize ("inplace" , [True , False ])
233
234
@pytest .mark .parametrize (
234
235
"original_dict, sorted_dict, ascending, ignore_index, output_index" ,
235
236
[
@@ -240,25 +241,28 @@ def test_sort_index_intervalindex(self):
240
241
],
241
242
)
242
243
def test_sort_index_ignore_index (
243
- self , original_dict , sorted_dict , ascending , ignore_index , output_index
244
+ self , inplace , original_dict , sorted_dict , ascending , ignore_index , output_index
244
245
):
245
246
# GH 30114
246
247
original_index = [2 , 5 , 3 ]
247
248
df = DataFrame (original_dict , index = original_index )
248
249
expected_df = DataFrame (sorted_dict , index = output_index )
249
-
250
- sorted_df = df .sort_index (ascending = ascending , ignore_index = ignore_index )
251
- tm .assert_frame_equal (sorted_df , expected_df )
252
- tm .assert_frame_equal (df , DataFrame (original_dict , index = original_index ))
253
-
254
- # Test when inplace is True
255
- copied_df = df .copy ()
256
- copied_df .sort_index (
257
- ascending = ascending , ignore_index = ignore_index , inplace = True
258
- )
259
- tm .assert_frame_equal (copied_df , expected_df )
250
+ kwargs = {
251
+ "ascending" : ascending ,
252
+ "ignore_index" : ignore_index ,
253
+ "inplace" : inplace ,
254
+ }
255
+
256
+ if inplace :
257
+ result_df = df .copy ()
258
+ result_df .sort_index (** kwargs )
259
+ else :
260
+ result_df = df .sort_index (** kwargs )
261
+
262
+ tm .assert_frame_equal (result_df , expected_df )
260
263
tm .assert_frame_equal (df , DataFrame (original_dict , index = original_index ))
261
264
265
+ @pytest .mark .parametrize ("inplace" , [True , False ])
262
266
@pytest .mark .parametrize (
263
267
"original_dict, sorted_dict, ascending, ignore_index, output_index" ,
264
268
[
@@ -293,21 +297,24 @@ def test_sort_index_ignore_index(
293
297
],
294
298
)
295
299
def test_sort_index_ignore_index_multi_index (
296
- self , original_dict , sorted_dict , ascending , ignore_index , output_index
300
+ self , inplace , original_dict , sorted_dict , ascending , ignore_index , output_index
297
301
):
298
302
# GH 30114, this is to test ignore_index on MulitIndex of index
299
303
mi = MultiIndex .from_tuples ([[2 , 1 ], [3 , 4 ]], names = list ("AB" ))
300
304
df = DataFrame (original_dict , index = mi )
301
305
expected_df = DataFrame (sorted_dict , index = output_index )
302
306
303
- sorted_df = df .sort_index (ascending = ascending , ignore_index = ignore_index )
304
- tm .assert_frame_equal (sorted_df , expected_df )
305
- tm .assert_frame_equal (df , DataFrame (original_dict , index = mi ))
307
+ kwargs = {
308
+ "ascending" : ascending ,
309
+ "ignore_index" : ignore_index ,
310
+ "inplace" : inplace ,
311
+ }
306
312
307
- # Test when inplace is True
308
- copied_df = df .copy ()
309
- copied_df .sort_index (
310
- ascending = ascending , ignore_index = ignore_index , inplace = True
311
- )
312
- tm .assert_frame_equal (copied_df , expected_df )
313
+ if inplace :
314
+ result_df = df .copy ()
315
+ result_df .sort_index (** kwargs )
316
+ else :
317
+ result_df = df .sort_index (** kwargs )
318
+
319
+ tm .assert_frame_equal (result_df , expected_df )
313
320
tm .assert_frame_equal (df , DataFrame (original_dict , index = mi ))
0 commit comments