inPlace= True vs False

Can anyone explain the benefits and main differences of using inPlace = True vs False. There are several different opinions in StackExchange on why it’s not a good idea to set it to True and that it should be deprecated. But DQ courses always recommend always set it to True.

Well, as far as I know, if you set it to True you do not need to assign the result to a new variable, therefore you save memory space.

If the method I need supports inplace, I always use it.

If you are working with a pandas dataframe and set inplace = True for the operation, the original pandas dataframe gets updated. For example if you use the df.dropna(), with inplace=True, your original dataframe gets modified with no missing values. It is advisable to always make a copy and make modifications to this.

1 Like

Using inplace depends on the task. If you need to have a source data frame to access later, you should not use inplace.
In addition if you are writing a large chain of transformations using dot annotation you should also not use inplace. Since when using inplace, the method returns None instead of DataFrame, and calling the next method in the chain will cause an error.

But in fact, your choice to use inplace or not will be based only on your convenience and style of writing code.
Since you can use just

data = data.method(inplace=False)

and it will be the same

1 Like