deep-copy

make a truly deep copy of a pandas Series

柔情痞子 提交于 2020-06-14 07:28:05
问题 I have a pd.Series with each cell being a list. I want to make a deep copy of it, however it seems like pd.Series.copy only creates a shallow copy of the values (even though the deep arg is True be default). example import pandas as pd sr = pd.Series([list(range(3)), list(range(3))]) sr_c = sr.copy() sr[0].append(4) the copied pd.Series sr_c is being transformed to 0 [0, 1, 2, 4] 1 [0, 1, 2] I did this and it worked: from copy import deepcopy sr_c = sr_c.apply(deepcopy) however this seems

make a truly deep copy of a pandas Series

蹲街弑〆低调 提交于 2020-06-14 07:28:03
问题 I have a pd.Series with each cell being a list. I want to make a deep copy of it, however it seems like pd.Series.copy only creates a shallow copy of the values (even though the deep arg is True be default). example import pandas as pd sr = pd.Series([list(range(3)), list(range(3))]) sr_c = sr.copy() sr[0].append(4) the copied pd.Series sr_c is being transformed to 0 [0, 1, 2, 4] 1 [0, 1, 2] I did this and it worked: from copy import deepcopy sr_c = sr_c.apply(deepcopy) however this seems

Avoid deepcopy due to performance

ε祈祈猫儿з 提交于 2020-05-29 05:09:30
问题 I have a list with another 3 lists in it. I need to do some operations on the lists, as i have like 180.000 of those the step of deepcopying already takes 2,5s to copy the lists once. The overall time including operations takes 80s out of 220s compution time. s = [[1,2,3], [4,5,6], [7,8,9]] s1 = copy.deepcopy(s) s1[0][0] = s[1][1] s1[1][1] = s[0][0] The shown operation needs to be repeated like a million of times. So deepcopy makes me face a performance bottleneck. Is there a more performant

AttributeError when using python deepcopy

我与影子孤独终老i 提交于 2020-05-13 07:02:39
问题 I have a class that has __eq__ and __hash__ overridden, to make its objects act as dictionary keys. Each object also carries a dictionary, keyed by other objects of the same class. I get a weird AttributeError when I try to deepcopy the whole structure. I am using Python 3.6.0 on OsX. From Python docs it looks as if deepcopy uses a memo dictionary to cache the objects it has already copied, so nested structures should not be a problem. What am I doing wrong then? Should I code up my own _

python list.copy shallow vs deep copy [duplicate]

被刻印的时光 ゝ 提交于 2020-05-01 21:01:11
问题 This question already has answers here : What is the difference between a deep copy and a shallow copy? (31 answers) What is the difference between shallow copy, deepcopy and normal assignment operation? (11 answers) Closed 2 years ago . Maybe I don't understand the definition of shallow copy... but i'm very confused: from the docs: Where "s" is a list (but same question applies to dictionaries respectively). "s.copy() | creates a shallow copy of s (same as s[:])" Except I thought s[:] was a

python list.copy shallow vs deep copy [duplicate]

喜你入骨 提交于 2020-05-01 20:51:31
问题 This question already has answers here : What is the difference between a deep copy and a shallow copy? (31 answers) What is the difference between shallow copy, deepcopy and normal assignment operation? (11 answers) Closed 2 years ago . Maybe I don't understand the definition of shallow copy... but i'm very confused: from the docs: Where "s" is a list (but same question applies to dictionaries respectively). "s.copy() | creates a shallow copy of s (same as s[:])" Except I thought s[:] was a

C++: Duplicating a tree of derived elements

独自空忆成欢 提交于 2020-02-25 13:06:05
问题 I have a base class and several derived classes. The base class looks like this: class Base { int type; //the derived type the object belongs to int nOfChildren; Base** children; //each child can be any of the derived types ... } Now I need to duplicate an instance of Base . Because of the recursion, a virtual method Base::duplicate() is needed. It also seems clear what should go in it: Base temp = new Base(); temp->type = temp; temp->nOfChildren = nOfChildren; temp->children = new Base*

Does a deep copy operation recursively copies subvariables which it doesn't own?

。_饼干妹妹 提交于 2020-02-03 05:42:05
问题 Given an object that has a variable which it doesn't own; that is, the variable is composed by aggregation instead of composition. Will a deep copy operation copy the variable or only the link to it? 回答1: I like the distinction that you are making here between the role of composition and aggregation in the context of a deep copy. I am going to go against the other answer and say: no, an object should not deep-copy another object that it doesn't own. One would expect a deep copy of an object

Copy constructor of Array List

情到浓时终转凉″ 提交于 2020-01-30 13:19:09
问题 I just read about deep copy of ArrayList, that people think new ArrayList<>(originalList); will create a shallow copy. And I wrote a small demo ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris")); List<String> copyNameList1=originalNameList; List<String> copyNameList2= new ArrayList<>(originalNameList); originalNameList.add("Duke"); copyNameList1.add("Ellen"); copyNameList1.set(1,"Bill"); copyNameList2.set(0,"Andy"); System.out.println(

Copy constructor of Array List

蓝咒 提交于 2020-01-30 13:18:23
问题 I just read about deep copy of ArrayList, that people think new ArrayList<>(originalList); will create a shallow copy. And I wrote a small demo ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris")); List<String> copyNameList1=originalNameList; List<String> copyNameList2= new ArrayList<>(originalNameList); originalNameList.add("Duke"); copyNameList1.add("Ellen"); copyNameList1.set(1,"Bill"); copyNameList2.set(0,"Andy"); System.out.println(