1import pytest 2 3from pandas import DataFrame 4import pandas._testing as tm 5 6 7class TestCopy: 8 @pytest.mark.parametrize("attr", ["index", "columns"]) 9 def test_copy_index_name_checking(self, float_frame, attr): 10 # don't want to be able to modify the index stored elsewhere after 11 # making a copy 12 ind = getattr(float_frame, attr) 13 ind.name = None 14 cp = float_frame.copy() 15 getattr(cp, attr).name = "foo" 16 assert getattr(float_frame, attr).name is None 17 18 def test_copy_cache(self): 19 # GH#31784 _item_cache not cleared on copy causes incorrect reads after updates 20 df = DataFrame({"a": [1]}) 21 22 df["x"] = [0] 23 df["a"] 24 25 df.copy() 26 27 df["a"].values[0] = -1 28 29 tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0]})) 30 31 df["y"] = [0] 32 33 assert df["a"].values[0] == -1 34 tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0], "y": [0]})) 35 36 def test_copy(self, float_frame, float_string_frame): 37 cop = float_frame.copy() 38 cop["E"] = cop["A"] 39 assert "E" not in float_frame 40 41 # copy objects 42 copy = float_string_frame.copy() 43 assert copy._mgr is not float_string_frame._mgr 44