Why does Pandas not come with an option to use .apply in place?

Viewed 198

I like to think every design decision is made for a reason. A lot of pandas functions (e.g. df.drop , df.rename df.replace) come with a parameter, inplace. If you set it to True, instead of returning a new dataframe, pandas modifies the dataframe, well, in place. No surprises here ;).

However, I often find my self using df.apply in combination with lambda expression to do somewhat more complex operations on columns. Consider the following example:

Say I have text data that needs to be pre-processed for sentiment analysis. I would use:

def remove_punctuation(text):
    no_punct = "".join([c for c in text if c not in string.punctuation])
    return no_punct

And then adapt my column as follows:

df['text'] = df['text'].apply(lambda x: remove_punctuation(x))

I recently noticed that .apply does not have an argument inplace=True. Since this function is mostly used to update dataframes, why is such an argument not available? What would be a rationale behind this?

1 Answers

pandas.DataFrame.apply and pandas.Series.apply both returns a Series either from a DataFrame or a Series. In your example you apply it to a Series and inplace might make sense there. However there are other applications where it wouldn't.

For example, with df being:

   col1  col2
0     1     3
1     2     4

Doing:

s = df.apply(lambda x: x.col1 + x.col2, axis=1)

Would return a Series which has different type and shape than the original DataFrame. In this case an inplace argument wouldn't make much sense.

I think pandas devs wanted to enforce consistency between pandas.DataFrame.apply and pandas.Series.apply, avoiding confusion generated by having an inplace argument in pandas.Series.apply only.

Related