UpSetPlot icon indicating copy to clipboard operation
UpSetPlot copied to clipboard

pandas FutureWarning resulting from chained assignment

Open verheytb opened this issue 1 year ago • 1 comments

Hello, I'm getting this error when working with the upsetplot package.

C:\Users\<username>\miniconda3\Lib\site-packages\upsetplot\plotting.py:795: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.

For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.


  styles["linewidth"].fillna(1, inplace=True)

verheytb avatar Jul 23 '24 16:07 verheytb

Hi everyone,

I'm also encountering the following warnings when using upsetplot:

/opt/conda/lib/python3.10/site-packages/upsetplot/plotting.py:795: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.

For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.

  styles["linewidth"].fillna(1, inplace=True)
/opt/conda/lib/python3.10/site-packages/upsetplot/plotting.py:796: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
...

This warning suggests that the current use of .fillna(..., inplace=True) modifies a copy rather than the original DataFrame or Series. With upcoming changes in pandas 3.0, this approach will no longer work.

Would it be possible to update the code to avoid chained assignment? The suggested approach would be to replace:

styles["linewidth"].fillna(1, inplace=True)

With:

styles["linewidth"] = styles["linewidth"].fillna(1)

This change should ensure compatibility with future versions of pandas and prevent the warning from appearing.

Thanks in advance for your attention to this!

AnthonyTedde avatar Oct 18 '24 13:10 AnthonyTedde