10

What is the guideline for using inplace?

For example,

df = df.reset_index()

or

df.reset_index(inplace=True)

Same same but different?

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
user3659451
  • 1,913
  • 9
  • 30
  • 43
  • 2
    Same thing. I prefer the first way for ease of reading next to other lines that don't act on a dataframe inplace. – itzy Dec 16 '15 at 19:37

1 Answers1

10

In terms of the resulting DataFrame df, the two approaches are the same. The difference lies in the (maximum) memory usage, since the in-place version does not create a copy of the DataFrame.

Consider this setup:

import numpy as np
import pandas as pd

def make_data():
    return pd.DataFrame(np.random.rand(1000000, 100))

def func_copy():
    df = make_data()
    df = df.reset_index()
    
def func_inplace():
    df = make_data()
    df.reset_index(inplace=True)

We can use the memory_profiler library to perform some benchmarking for the memory usage:

%load_ext memory_profiler

%memit func_copy()
# peak memory: 1602.66 MiB, increment: 1548.66 MiB

%memit func_inplace()
# peak memory: 817.02 MiB, increment: 762.94 MiB

As expected, the in-place version is more memory efficient.

On the other hand, there also seems to be a non-trivial difference in running time between the approaches when the data size is large enough (e.g. in the above example):

%timeit func_copy()
1 loops, best of 3: 2.56 s per loop

%timeit func_inplace()
1 loops, best of 3: 1.35 s per loop

These differences may or may not be significant depending on the use case (e.g. adhoc exploratory analysis vs. production code), data size and the hardware resource available. In general, it might be a good idea to use the in-place version whenever possible for better memory and run time efficiency.

blong
  • 2,815
  • 8
  • 44
  • 110
YS-L
  • 14,358
  • 3
  • 47
  • 58