1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
import pytest
 
import pandas.util._test_decorators as td
 
from pandas import DataFrame
import pandas._testing as tm
 
 
class TestCopy:
    @pytest.mark.parametrize("attr", ["index", "columns"])
    def test_copy_index_name_checking(self, float_frame, attr):
        # don't want to be able to modify the index stored elsewhere after
        # making a copy
        ind = getattr(float_frame, attr)
        ind.name = None
        cp = float_frame.copy()
        getattr(cp, attr).name = "foo"
        assert getattr(float_frame, attr).name is None
 
    @td.skip_copy_on_write_invalid_test
    def test_copy_cache(self):
        # GH#31784 _item_cache not cleared on copy causes incorrect reads after updates
        df = DataFrame({"a": [1]})
 
        df["x"] = [0]
        df["a"]
 
        df.copy()
 
        df["a"].values[0] = -1
 
        tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0]}))
 
        df["y"] = [0]
 
        assert df["a"].values[0] == -1
        tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0], "y": [0]}))
 
    def test_copy(self, float_frame, float_string_frame):
        cop = float_frame.copy()
        cop["E"] = cop["A"]
        assert "E" not in float_frame
 
        # copy objects
        copy = float_string_frame.copy()
        assert copy._mgr is not float_string_frame._mgr
 
    @td.skip_array_manager_invalid_test
    def test_copy_consolidates(self):
        # GH#42477
        df = DataFrame(
            {
                "a": np.random.randint(0, 100, size=55),
                "b": np.random.randint(0, 100, size=55),
            }
        )
 
        for i in range(0, 10):
            df.loc[:, f"n_{i}"] = np.random.randint(0, 100, size=55)
 
        assert len(df._mgr.blocks) == 11
        result = df.copy()
        assert len(result._mgr.blocks) == 1