1. Home
  2. Data Science
  3. Pandas
  4. What caveat applies to df.copy(deep=True) for a column of…

Added 30 Nov -0001

What caveat applies to df.copy(deep=True) for a column of dtype object holding Python lists?

  1. The data and index are copied, but the Python objects inside object columns are not recursively copied, so they remain shared Correct answer
  2. deep=True is not allowed for object dtype columns, because pandas cannot deep-copy arbitrary Python objects
  3. Every nested list is deep-copied, guaranteeing full independence, so mutating one frame never affects the contents of the other
  4. deep=True copies only the index and shares all of the column data, while deep=False copies the column arrays

Correct answer

A. The data and index are copied, but the Python objects inside object columns are not recursively copied, so they remain shared

23 views

MCQSEXAM

Explanation

Deep copy semantics: pandas copies the NumPy arrays and the index, but an object array holds references, so mutable Python objects inside remain shared between the two frames. Mutating a list inside a copied frame can therefore affect the original. Genuine independence requires copy.deepcopy or rebuilding the column as new objects.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Pandas MCQs

Practise all Pandas MCQs

Similar MCQs