I am trying to create a copy of a pandas series whose elements contain instances of a given class. I do so with the following command
duplicateSeries = originalSeries.copy()
Now, when i go to make changes in the elements of the duplicateSeries, such as changes in a series within a class instance, the corresponding elements of the originalSeries also gets updated.
How can I prevent this from happening?
1
The objects inside both the original and the copied Series still reference the same memory location (so the same objects).
To solve it just create a deepcopy, that copy not only the Series structure but the objects inside:
import copy
duplicateSeries = originalSeries.apply(copy.deepcopy)