w3resource

Pandas Series: copy() function

Copy of Pandas object

The copy() function is used to make a copy of Pandas object’s indices and data.

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).

Syntax:

Series.copy(self, deep=True)
Pandas Series copy() function

Parameters:

Name Description Type Default Value Required / Optional
deep Make a deep copy, including a copy of the data and the indices. With deep=False neither the indices nor the data are copied. bool True Required

Returns: copy - Series or DataFrame
Object type matches caller.

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.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], index=["p", "q"])
s

Output:

p    2
q    3
dtype: int64
Pandas Series copy() function

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], index=["p", "q"])
s_copy = s.copy()
s_copy

Output:

p    2
q    3
dtype: int64

Example - Shallow copy versus default (deep) copy:

Shallow copy shares data and index with original.

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], index=["p", "q"])
deep = s.copy()
shallow = s.copy(deep=False)
s is shallow

Output:

False

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], index=["p", "q"])
deep = s.copy()
shallow = s.copy(deep=False)
s.values is shallow.values and s.index is shallow.index

Output:

True

Example - Deep copy has own copy of data and index:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], index=["p", "q"])
deep = s.copy()
shallow = s.copy(deep=False)
s is deep

Output:

False

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], index=["p", "q"])
deep = s.copy()
shallow = s.copy(deep=False)
s.values is deep.values or s.index is deep.index

Output:

False

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

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], index=["p", "q"])
deep = s.copy()
shallow = s.copy(deep=False)
s[0] = 4
shallow[1] = 5
s

Output:

p    4
q    5
dtype: int64
Pandas Series copy() function

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], index=["p", "q"])
deep = s.copy()
shallow = s.copy(deep=False)
s[0] = 4
shallow[1] = 5
shallow

Output:

p    4
q    5
dtype: int64
Pandas Series copy() function

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], index=["p", "q"])
deep = s.copy()
deep

Output:

p    2
q    3
dtype: int64
Pandas Series copy() function

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.

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([[2, 3], [4, 5]])
deep = s.copy()
s[0][0] = 10
s

Output:

0    [10, 3]
1     [4, 5]
dtype: object
Pandas Series copy() function

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([[2, 3], [4, 5]])
deep = s.copy()
s[0][0] = 10
deep

Output:

0    [10, 3]
1     [4, 5]
dtype: object

Previous: Better dtypes for object columns
Next: Values in Pandas Series or Index



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-copy.php