pandas 1.4.2

NotesParametersReturns
copy(self: 'NDFrameT', deep: 'bool_t' = True) -> 'NDFrameT'

When deep=True (default), a new object will be created with a copy of the calling object's data and indices. Modifications to the data or indices of the copy will not be reflected in the original object (see notes below).

When deep=False , a new object will be created without copying the calling object's data or index (only references to the data and index are copied). Any changes to the data of the original will be reflected in the shallow copy (and vice versa).

Notes

When deep=True , data is copied but actual Python objects will not be copied recursively, only the reference to the object. This is in contrast to copy.deepcopy in the Standard Library, which recursively copies object data (see examples below).

While Index objects are copied when deep=True , the underlying numpy array is not copied for performance reasons. Since Index is immutable, the underlying data can be safely shared and a copy is not needed.

Parameters

deep : bool, default True

Make a deep copy, including a copy of the data and the indices. With deep=False neither the indices nor the data are copied.

Returns

copy : Series or DataFrame

Object type matches caller.

Make a copy of this object's indices and data.

Examples

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series([1, 2], index=["a", "b"])
... s a 1 b 2 dtype: int64
This example is valid syntax, but we were not able to check execution
>>> s_copy = s.copy()
... s_copy a 1 b 2 dtype: int64

Shallow copy versus default (deep) copy:

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series([1, 2], index=["a", "b"])
... deep = s.copy()
... shallow = s.copy(deep=False)

Shallow copy shares data and index with original.

This example is valid syntax, but we were not able to check execution
>>> s is shallow
False
This example is valid syntax, but we were not able to check execution
>>> s.values is shallow.values and s.index is shallow.index
True

Deep copy has own copy of data and index.

This example is valid syntax, but we were not able to check execution
>>> s is deep
False
This example is valid syntax, but we were not able to check execution
>>> s.values is deep.values or s.index is deep.index
False

Updates to the data shared by shallow copy and original is reflected in both; deep copy remains unchanged.

This example is valid syntax, but we were not able to check execution
>>> s[0] = 3
... shallow[1] = 4
... s a 3 b 4 dtype: int64
This example is valid syntax, but we were not able to check execution
>>> shallow
a    3
b    4
dtype: int64
This example is valid syntax, but we were not able to check execution
>>> deep
a    1
b    2
dtype: int64

Note that when copying an object containing Python objects, a deep copy will copy the data, but will not do so recursively. Updating a nested data object will be reflected in the deep copy.

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series([[1, 2], [3, 4]])
... deep = s.copy()
... s[0][0] = 10
... s 0 [10, 2] 1 [3, 4] dtype: object
This example is valid syntax, but we were not able to check execution
>>> deep
0    [10, 2]
1     [3, 4]
dtype: object
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#5926
type: <class 'function'>
Commit: