Repository: pandas-dev/pandas
File: pandas/tests/frame/methods/test_rename_axis.py (Python)

Code before:
)
import pandas._testing as tm


class TestDataFrameRenameAxis:
    def test_rename_axis_inplace(self, float_frame):
        # GH#15704
        expected = float_frame.rename_axis("foo")
        result = float_frame.copy()
        return_value = no_return = result.rename_axis("foo", inplace=True)
        assert return_value is None

        assert no_return is None
        tm.assert_frame_equal(result, expected)

        expected = float_frame.rename_axis("bar", axis=1)
        result = float_frame.copy()
        return_value = no_return = result.rename_axis("bar", axis=1, inplace=True)
        assert return_value is None

        assert no_return is None
        tm.assert_frame_equal(result, expected)

    def test_rename_axis_with_allows_duplicate_labels_false(self):
        # GH#44958
        # Test that rename_axis() works correctly when allows_duplicate_labels=False
        df = DataFrame([[1, 2], [3, 4]], columns=["a", "b"]).set_flags(
            allows_duplicate_labels=False
        )

        # Test renaming index (axis=0) with allows_duplicate_labels=False
        result = df.rename_axis("idx", axis=0)
        expected = DataFrame(
            [[1, 2], [3, 4]], index=Index([0, 1], name="idx"), columns=["a", "b"]
        ).set_flags(allows_duplicate_labels=False)
        tm.assert_frame_equal(result, expected)

    def test_rename_axis_raises(self):
        # GH#17833
        df = DataFrame({"A": [1, 2], "B": [1, 2]})
        with pytest.raises(ValueError, match="Use `.rename`"):
            df.rename_axis(id, axis=0)

        with pytest.raises(ValueError, match="Use `.rename`"):
            df.rename_axis({0: 10, 1: 20}, axis=0)

        with pytest.raises(ValueError, match="Use `.rename`"):
            df.rename_axis(id, axis=1)

        with pytest.raises(ValueError, match="Use `.rename`"):
            df["A"].rename_axis(id)

Review comment (suggestion):
@kubaszkliniarz, thanks for adding the test.

I suggest removing lines with the comments, except for the line with the reference `GH#44958` to the original issue.

Code after:
)
import pandas._testing as tm


class TestDataFrameRenameAxis:
    def test_rename_axis_inplace(self, float_frame):
        # GH#15704
        expected = float_frame.rename_axis("foo")
        result = float_frame.copy()
        return_value = no_return = result.rename_axis("foo", inplace=True)
        assert return_value is None

        assert no_return is None
        tm.assert_frame_equal(result, expected)

        expected = float_frame.rename_axis("bar", axis=1)
        result = float_frame.copy()
        return_value = no_return = result.rename_axis("bar", axis=1, inplace=True)
        assert return_value is None

        assert no_return is None
        tm.assert_frame_equal(result, expected)

    def test_rename_axis_with_allows_duplicate_labels_false(self):
        # GH#44958
        df = DataFrame([[1, 2], [3, 4]], columns=["a", "b"]).set_flags(
            allows_duplicate_labels=False
        )

        result = df.rename_axis("idx", axis=0)
        expected = DataFrame(
            [[1, 2], [3, 4]], index=Index([0, 1], name="idx"), columns=["a", "b"]
        )
        tm.assert_frame_equal(result, expected, check_flags=False)

    def test_rename_axis_raises(self):
        # GH#17833
        df = DataFrame({"A": [1, 2], "B": [1, 2]})
        with pytest.raises(ValueError, match="Use `.rename`"):
            df.rename_axis(id, axis=0)

        with pytest.raises(ValueError, match="Use `.rename`"):
            df.rename_axis({0: 10, 1: 20}, axis=0)

        with pytest.raises(ValueError, match="Use `.rename`"):
            df.rename_axis(id, axis=1)

        with pytest.raises(ValueError, match="Use `.rename`"):
            df["A"].rename_axis(id)

    def test_rename_axis_mapper(self):