0

I stumbled upon the issue, that deepcopy does not actually copy the whole object with "on-the-fly" added attributes. Why is that and is there any solution to it?

Example:

import pandas as pd
from copy import deepcopy
frame_one = pd.DataFrame({'hello': [1,2,3], 'world': [4,5,6]})
frame_one.name = 'foo'
frame_two = deepcopy(frame_one)
frame_two.name

Error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "a_path\pandas\core\generic.py", line 5478, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'name'
martineau
  • 119,623
  • 25
  • 170
  • 301
Pm740
  • 339
  • 2
  • 12
  • 2
    This behavior is specific to pandas' data frames. Is your problem specific to data frame or not? If so, please edit your title and question. – Holt Sep 02 '21 at 09:21
  • 1
    the issue is not with deepcopy. its with dataframe. its not able to add a attribute called name – Daniel Paul Sep 02 '21 at 09:22
  • Your program doesn't even go to the deepcopy line. You have a problem with adding dynamically an attribute. You should read https://stackoverflow.com/questions/1325673/how-to-add-property-to-a-class-dynamically – RobBlanchard Sep 02 '21 at 09:27
  • 2
    That's not actually true - if you try running the code the OP posted you'll see that `frame_one.name = 'foo'` runs fine, the error comes after the deepcopy when the attribute is accessed on `frame_two.name` – mdmjsh Sep 02 '21 at 09:32
  • 2
    Just to be clear: The *specific* issue you are showing is because every object can define *for itself* what ``deepcopy`` means, and ``pandas`` decided that ``deepcopy`` of a dataframe means to *ignore* such attributes. – MisterMiyagi Sep 02 '21 at 09:33

1 Answers1

1

Those attributes don't get copied because pandas overrides deepcopy.

You can add this after the first deepcopy:

frame_two.__dict__ = deepcopy(frame_one.__dict__)
steamdragon
  • 1,052
  • 13
  • 15