@@ -230,6 +230,7 @@ def test_sort_index_intervalindex(self):
230230 result = result .columns .levels [1 ].categories
231231 tm .assert_index_equal (result , expected )
232232
233+ @pytest .mark .parametrize ("inplace" , [True , False ])
233234 @pytest .mark .parametrize (
234235 "original_dict, sorted_dict, ascending, ignore_index, output_index" ,
235236 [
@@ -240,25 +241,28 @@ def test_sort_index_intervalindex(self):
240241 ],
241242 )
242243 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
244245 ):
245246 # GH 30114
246247 original_index = [2 , 5 , 3 ]
247248 df = DataFrame (original_dict , index = original_index )
248249 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 )
260263 tm .assert_frame_equal (df , DataFrame (original_dict , index = original_index ))
261264
265+ @pytest .mark .parametrize ("inplace" , [True , False ])
262266 @pytest .mark .parametrize (
263267 "original_dict, sorted_dict, ascending, ignore_index, output_index" ,
264268 [
@@ -293,21 +297,24 @@ def test_sort_index_ignore_index(
293297 ],
294298 )
295299 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
297301 ):
298302 # GH 30114, this is to test ignore_index on MulitIndex of index
299303 mi = MultiIndex .from_tuples ([[2 , 1 ], [3 , 4 ]], names = list ("AB" ))
300304 df = DataFrame (original_dict , index = mi )
301305 expected_df = DataFrame (sorted_dict , index = output_index )
302306
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+ }
306312
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 )
313320 tm .assert_frame_equal (df , DataFrame (original_dict , index = mi ))
0 commit comments