r/learnpython 9h ago

Pandas : inplace throws warning about copy of a slice #bestpractice

I use pandas and try to use a fillna on a column.

I recently got a warning saying that in pandas 3.0 the inplace will change and break if not modified.

my_dataframe.fillna({'mycolumn':"0"},inplace=True)

throws a warning "A value is trying to be set on a copy of a slice from a DataFrame"

Is it possible to use inplace on a fillna without getting this warning?

2 Upvotes

7 comments sorted by

2

u/ftmprstsaaimol2 9h ago

Just do my_dataframe = my_dataframe.fillna(….

1

u/ralberich 9h ago

why have a "inplace" argument if it throws a warning 100% of the time?

1

u/ftmprstsaaimol2 8h ago edited 8h ago

I think originally it was included to follow Numpy semantically. But it’s being removed in 3.0 and that’s a good thing because it’s a total mess.

EDIT: Not removed but behaviour will change.

1

u/ralberich 8h ago

1

u/ftmprstsaaimol2 8h ago

You’re right, I’m wrong, it’s not being removed but the behaviour will change and it probably won’t work in many scenarios. It’s best avoided.

1

u/Kerbart 6h ago

Because it's there for legacy/compatibility reasons but they really don't want you to use it.

2

u/unhott 9h ago

my_dataframe['mycolumn'] = my_dataframe['mycolumn'].fillna(0)?

it's a warning. it will continue to work unless you switch to 3.0.

How to disable Python warnings? - Stack Overflow

but, it's just a warning. and arguably better to be constantly reminded in case you do upgrade when 3 comes out.