Examples
import numpy as np
import pandas as pd
s = pd.Series([2, 3], index=["p", "q"])
s
s_copy = s.copy()
s_copy
Shallow copy versus default (deep) copy:
s = pd.Series([2, 3], index=["p", "q"])
deep = s.copy()
shallow = s.copy(deep=False)
Shallow copy shares data and index with original.
s is shallow
s.values is shallow.values and s.index is shallow.index
Deep copy has own copy of data and index.
s is deep
s.values is deep.values or s.index is deep.index
Updates to the data shared by shallow copy and original is reflected in both;
deep copy remains unchanged:
s[0] = 4
shallow[1] = 5
s
shallow
deep
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.
s = pd.Series([[2, 3], [4, 5]])
deep = s.copy()
s[0][0] = 8
s
deep