diff --git a/pandas/tests/frame/methods/test_drop_duplicates.py b/pandas/tests/frame/methods/test_drop_duplicates.py index 29ab2e1bfd512..0856ed6885978 100644 --- a/pandas/tests/frame/methods/test_drop_duplicates.py +++ b/pandas/tests/frame/methods/test_drop_duplicates.py @@ -393,6 +393,7 @@ def test_drop_duplicates_inplace(): tm.assert_frame_equal(result, expected) +@pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "origin_dict, output_dict, ignore_index, output_index", [ @@ -403,24 +404,17 @@ def test_drop_duplicates_inplace(): ], ) def test_drop_duplicates_ignore_index( - origin_dict, output_dict, ignore_index, output_index + inplace, origin_dict, output_dict, ignore_index, output_index ): # GH 30114 df = DataFrame(origin_dict) expected = DataFrame(output_dict, index=output_index) - # Test when inplace is False - result = df.drop_duplicates(ignore_index=ignore_index) - tm.assert_frame_equal(result, expected) - - # to verify original dataframe is not mutated - tm.assert_frame_equal(df, DataFrame(origin_dict)) - - # Test when inplace is True - copied_df = df.copy() - - copied_df.drop_duplicates(ignore_index=ignore_index, inplace=True) - tm.assert_frame_equal(copied_df, expected) + if inplace: + result_df = df.copy() + result_df.drop_duplicates(ignore_index=ignore_index, inplace=inplace) + else: + result_df = df.drop_duplicates(ignore_index=ignore_index, inplace=inplace) - # to verify that input is unchanged + tm.assert_frame_equal(result_df, expected) tm.assert_frame_equal(df, DataFrame(origin_dict)) diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index 6866aab11d2fa..29a52de66e100 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -230,6 +230,7 @@ def test_sort_index_intervalindex(self): result = result.columns.levels[1].categories tm.assert_index_equal(result, expected) + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_dict, sorted_dict, ascending, ignore_index, output_index", [ @@ -240,25 +241,28 @@ def test_sort_index_intervalindex(self): ], ) def test_sort_index_ignore_index( - self, original_dict, sorted_dict, ascending, ignore_index, output_index + self, inplace, original_dict, sorted_dict, ascending, ignore_index, output_index ): # GH 30114 original_index = [2, 5, 3] df = DataFrame(original_dict, index=original_index) expected_df = DataFrame(sorted_dict, index=output_index) - - sorted_df = df.sort_index(ascending=ascending, ignore_index=ignore_index) - tm.assert_frame_equal(sorted_df, expected_df) - tm.assert_frame_equal(df, DataFrame(original_dict, index=original_index)) - - # Test when inplace is True - copied_df = df.copy() - copied_df.sort_index( - ascending=ascending, ignore_index=ignore_index, inplace=True - ) - tm.assert_frame_equal(copied_df, expected_df) + kwargs = { + "ascending": ascending, + "ignore_index": ignore_index, + "inplace": inplace, + } + + if inplace: + result_df = df.copy() + result_df.sort_index(**kwargs) + else: + result_df = df.sort_index(**kwargs) + + tm.assert_frame_equal(result_df, expected_df) tm.assert_frame_equal(df, DataFrame(original_dict, index=original_index)) + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_dict, sorted_dict, ascending, ignore_index, output_index", [ @@ -293,21 +297,24 @@ def test_sort_index_ignore_index( ], ) def test_sort_index_ignore_index_multi_index( - self, original_dict, sorted_dict, ascending, ignore_index, output_index + self, inplace, original_dict, sorted_dict, ascending, ignore_index, output_index ): # GH 30114, this is to test ignore_index on MulitIndex of index mi = MultiIndex.from_tuples([[2, 1], [3, 4]], names=list("AB")) df = DataFrame(original_dict, index=mi) expected_df = DataFrame(sorted_dict, index=output_index) - sorted_df = df.sort_index(ascending=ascending, ignore_index=ignore_index) - tm.assert_frame_equal(sorted_df, expected_df) - tm.assert_frame_equal(df, DataFrame(original_dict, index=mi)) + kwargs = { + "ascending": ascending, + "ignore_index": ignore_index, + "inplace": inplace, + } - # Test when inplace is True - copied_df = df.copy() - copied_df.sort_index( - ascending=ascending, ignore_index=ignore_index, inplace=True - ) - tm.assert_frame_equal(copied_df, expected_df) + if inplace: + result_df = df.copy() + result_df.sort_index(**kwargs) + else: + result_df = df.sort_index(**kwargs) + + tm.assert_frame_equal(result_df, expected_df) tm.assert_frame_equal(df, DataFrame(original_dict, index=mi)) diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index e733c01e01740..ecf2fcf90dd2d 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -461,6 +461,7 @@ def test_sort_values_na_position_with_categories_raises(self): with pytest.raises(ValueError): df.sort_values(by="c", ascending=False, na_position="bad_position") + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_dict, sorted_dict, ignore_index, output_index", [ @@ -481,24 +482,18 @@ def test_sort_values_na_position_with_categories_raises(self): ], ) def test_sort_values_ignore_index( - self, original_dict, sorted_dict, ignore_index, output_index + self, inplace, original_dict, sorted_dict, ignore_index, output_index ): # GH 30114 df = DataFrame(original_dict) expected = DataFrame(sorted_dict, index=output_index) + kwargs = {"ignore_index": ignore_index, "inplace": inplace} - # Test when inplace is False - sorted_df = df.sort_values("A", ascending=False, ignore_index=ignore_index) - tm.assert_frame_equal(sorted_df, expected) - - tm.assert_frame_equal(df, DataFrame(original_dict)) - - # Test when inplace is True - copied_df = df.copy() - - copied_df.sort_values( - "A", ascending=False, ignore_index=ignore_index, inplace=True - ) - tm.assert_frame_equal(copied_df, expected) + if inplace: + result_df = df.copy() + result_df.sort_values("A", ascending=False, **kwargs) + else: + result_df = df.sort_values("A", ascending=False, **kwargs) + tm.assert_frame_equal(result_df, expected) tm.assert_frame_equal(df, DataFrame(original_dict)) diff --git a/pandas/tests/series/methods/test_sort_index.py b/pandas/tests/series/methods/test_sort_index.py index a9b73c2344681..5d47c54c9c336 100644 --- a/pandas/tests/series/methods/test_sort_index.py +++ b/pandas/tests/series/methods/test_sort_index.py @@ -136,6 +136,7 @@ def test_sort_index_intervals(self): ) tm.assert_series_equal(result, expected) + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_list, sorted_list, ascending, ignore_index, output_index", [ @@ -146,23 +147,22 @@ def test_sort_index_intervals(self): ], ) def test_sort_index_ignore_index( - self, original_list, sorted_list, ascending, ignore_index, output_index + self, inplace, original_list, sorted_list, ascending, ignore_index, output_index ): # GH 30114 ser = Series(original_list) expected = Series(sorted_list, index=output_index) - - # Test when inplace is False - sorted_sr = ser.sort_index(ascending=ascending, ignore_index=ignore_index) - tm.assert_series_equal(sorted_sr, expected) - - tm.assert_series_equal(ser, Series(original_list)) - - # Test when inplace is True - copied_sr = ser.copy() - copied_sr.sort_index( - ascending=ascending, ignore_index=ignore_index, inplace=True - ) - tm.assert_series_equal(copied_sr, expected) - + kwargs = { + "ascending": ascending, + "ignore_index": ignore_index, + "inplace": inplace, + } + + if inplace: + result_ser = ser.copy() + result_ser.sort_index(**kwargs) + else: + result_ser = ser.sort_index(**kwargs) + + tm.assert_series_equal(result_ser, expected) tm.assert_series_equal(ser, Series(original_list)) diff --git a/pandas/tests/series/methods/test_sort_values.py b/pandas/tests/series/methods/test_sort_values.py index 2cea6f061de76..6dc63986ef144 100644 --- a/pandas/tests/series/methods/test_sort_values.py +++ b/pandas/tests/series/methods/test_sort_values.py @@ -157,6 +157,7 @@ def test_sort_values_categorical(self): expected = df.iloc[[2, 1, 5, 4, 3, 0]] tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_list, sorted_list, ignore_index, output_index", [ @@ -165,21 +166,18 @@ def test_sort_values_categorical(self): ], ) def test_sort_values_ignore_index( - self, original_list, sorted_list, ignore_index, output_index + self, inplace, original_list, sorted_list, ignore_index, output_index ): # GH 30114 - sr = Series(original_list) + ser = Series(original_list) expected = Series(sorted_list, index=output_index) + kwargs = {"ignore_index": ignore_index, "inplace": inplace} - # Test when inplace is False - sorted_sr = sr.sort_values(ascending=False, ignore_index=ignore_index) - tm.assert_series_equal(sorted_sr, expected) + if inplace: + result_ser = ser.copy() + result_ser.sort_values(ascending=False, **kwargs) + else: + result_ser = ser.sort_values(ascending=False, **kwargs) - tm.assert_series_equal(sr, Series(original_list)) - - # Test when inplace is True - copied_sr = sr.copy() - copied_sr.sort_values(ascending=False, ignore_index=ignore_index, inplace=True) - tm.assert_series_equal(copied_sr, expected) - - tm.assert_series_equal(sr, Series(original_list)) + tm.assert_series_equal(result_ser, expected) + tm.assert_series_equal(ser, Series(original_list))