SettingWithCopyWarning when patching a ColumnDataSource

Relevant Versions: Bokeh 2.2.3, Python 3.8.5, Mac OSX Big Sur

Issue Summary: A complex data analysis and visualization app includes some functionality to patch source data used for UIs and plots based on user selections.

The very first time one of these patch actions occurs on a data source, the Python terminal for the bokeh shows a warning related to Pandas dataframes and setting-with-copy. See below for log info.

/Users/x/opt/anaconda3/lib/python3.8/site-packages/bokeh/core/property/wrappers.py:463: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self[name][ind] = value

The referenced line number (wrappers.py:463) confirms other debugging steps that this is happening in the bokeh patch operation. Despite the warning, the code appears to work as expected.

The warning does not appear on any subsequent interactions or patching operations.

My data setup does include as Pandas dataframe, which is wrapped in ColumnDataSource() for the plots and UIs that require access. I did try to change this to a dict instead, but still encountered the error.

Unfortunately, I have not been able to generate a smaller example to expose the observed behavior.

@_jm it’s hard to say much without a reproducer, but if you wanted to experiment, the appropriate change is probably (maybe?) to assign from .loc whenever self.name is a pandas series, instead of using the bracket getitem syntax.

Understood.

The offending assignment is happening in the bokeh wrapper.py module in a _patch() function. I am not directly updating a pandas dataframe in my code.

I am applying a data patch to the source with the following statement to update a slice of several variables in my data, e.g.

data_patch = {
    TS_EVENTS_EVENT_COLUMN_NAME: [(slice(ii0, ii1, 1), [mnvr] * (ii1-ii0))],
    'line_width_full': [(slice(ii0, ii1, 1), [line_width_full] * (ii1-ii0))],
    'line_width_zoom': [(slice(ii0, ii1, 1), [line_width_zoom] * (ii1-ii0))],
    }
source.patch(data_patch)

where (ii0, ii1) are indices computed earlier and source is the ColumnDataSource. This assignment generates the warning the first time through, but subsequent invocations of the callback by user actions do not.

Right, I meant if you want to experiment modifying the Bokeh code in wrapper.py

As I understand this warning, it signals that code may or may not behave according to expectation. It seems that in our case, it is behaving as expected (there are lots of tests to ensure that patching functions) but I can appreciate that the warning is annoying. Pandas may also only issue the warning once.

1 Like