79147328

Date: 2024-11-01 08:19:42
Score: 0.5
Natty:
Report link

(To summarise the answers in the comments)

deepcopy() is (waaaayyyy) faster on DataFrames compared to lists because of how pandas stores data: DataFrames use contiguous memory blocks (thanks to numpy) for each column, which allows deepcopy() to copy everything in one quick operation instead of iterating through all elements.

Python lists are essentially pointers to separate objects scattered across memory. Copying a large list means handling each item individually, which is way slower and involves more memory.

Since deepcopy() just calls DataFrame.deepcopy, which uses numpy.ndarray.copy, it doesn’t have to go element by element.

Reasons:
  • Blacklisted phrase (0.5): thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: mpivet-p