Skip to content

Copy method does not make truly deep copies of dtype object arrays #12663

@agartland

Description

@agartland

The copy method of pd.Series and pd.DataFrame has a parameter deep which claims to Make a deep copy, i.e. also copy data. The example below seems to show that this isn't a truly deep copy (as in from copy import deepcopy) I can't seem to find the implementation in the source. I am wondering if this behavior below is expected, if something different should be done for dtype=object to make it a truly deep copy, or if we could at least add a note to the documentation that notes this behavior?

Thanks!

import pandas as pd
import copy
import numpy as np

"""Create a Series based on np array of objects"""
a = pd.Series(np.array([1, 'series of objects', {'first':3,'second':5}], dtype=np.object))
b = a.copy(deep=True)
c = copy.deepcopy(a)

"""The dict has length 2"""
print len(a.loc[2]), len(b.loc[2]), len(c.loc[2])

"""Remove one key from c (the deepcopy)"""
c.loc[2].pop('first')

"""Only changes c"""
print len(a.loc[2]), len(b.loc[2]), len(c.loc[2])

"""Remove one key from c (the pandas deepcopy)"""
b.loc[2].pop('first')

"""Changes a and b?"""
print len(a.loc[2]), len(b.loc[2]), len(c.loc[2])

Metadata

Metadata

Assignees

No one assigned

    Labels

    Compatpandas objects compatability with Numpy or Python functionsDocsUsage Question

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions