Does pandas.DataFrame.drop(..., inplace=True) use extra memory?

Viewed 202

Does drop() function make a copy of the data frame from which the values are dropped while doing the dropping operation? If so, is this true even if inplace=True is set? I don't see a reason why copying would be needed, but still want to make sure in order to optimize memory footprint of my code.

1 Answers

that is really cool question, and let's consider some facts then. First we'll create a dummy DataFrame object:

import numpy as np
df = pd.DataFrame(np.random.randn(5, 4), columns=list('ABCD'))

then lets read its parameters:

import sys
print(sys.getsizeof(df))
print(id(df))
>>320
>>4789316624

knowing those parameters lets read "dropped" object parameters:

print(sys.getsizeof(df.drop('A', axis=1)))
print(id(df.drop('A', axis=1)))
>>280
>>4793436944

and compare it with "dropped inplace" object:

df.drop('A', axis=1, inplace=True)
print(sys.getsizeof(df))
print(id(df))
>>280
>>4789316624

This proves sizes of object in both approaches decrease, but the memory address does not change in "inplaced" object, so there is extra memory in use.

Related