Bokeh.models ColumnDataSource not accepting pandas dataframes?

Hey guys just testing out using bokeh to visualise a network

have been trying to use pandas dataframe in ColumnDataSource and its giving me an error that doesn’t make sense

from bokeh.models import ColumnDataSource

code below

from bokeh.models import ColumnDataSource
import numpy as np
import pandas as pd

df = pd.DataFrame(data=np.zeros((8,8)), columns=["a", "b", "c", "d", "e", "f", "g", "h"])

source = ColumnDataSource(data=df)

error from this

--------------------------------------------------------------------------

ValueError: expected a dict or pandas.DataFrame, got      a    b    c    d    e    f    g    h
0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
3  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
4  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
5  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
6  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
7  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0

any idea whats the reason for this?

You code works fine (as expected) for me with Boekh 2.4.3:

In [1]: from bokeh.models import ColumnDataSource
   ...: import numpy as np
   ...: import pandas as pd
   ...:
   ...: df = pd.DataFrame(data=np.zeros((8,8)), columns=["a", "b", "c", "d", "e", "f", "g", "h"])
   ...:
   ...: source = ColumnDataSource(data=df)

In [2]: source.data
Out[2]:
{'index': array([0, 1, 2, 3, 4, 5, 6, 7]),
 'a': array([0., 0., 0., 0., 0., 0., 0., 0.]),
 'b': array([0., 0., 0., 0., 0., 0., 0., 0.]),
 'c': array([0., 0., 0., 0., 0., 0., 0., 0.]),
 'd': array([0., 0., 0., 0., 0., 0., 0., 0.]),
 'e': array([0., 0., 0., 0., 0., 0., 0., 0.]),
 'f': array([0., 0., 0., 0., 0., 0., 0., 0.]),
 'g': array([0., 0., 0., 0., 0., 0., 0., 0.]),

Please provide more complete information (e.g. relevant package versions)

here are the relevant package versions

bokeh 2.4.3
numpy 1.23.1
pandas 1.4.3

@liankai_neow I still cannot reproduce any problem whatsoever with those versions and Python 3.10:

In [4]: from bokeh.models import ColumnDataSource
   ...: import numpy as np
   ...: import pandas as pd
   ...:
   ...: df = pd.DataFrame(data=np.zeros((8,8)), columns=["a", "b", "c", "d", "e", "f", "g", "h"])
   ...:
   ...: source = ColumnDataSource(data=df)

In [5]: np.__version__
Out[5]: '1.23.1'

In [6]: pd.__version__
Out[6]: '1.4.3'

In [7]: import bokeh; bokeh.__version__
Out[7]: '2.4.3'

So you are going to have to be even more specific:

  • System/OS
  • Python version
  • How you are running the code (exactly what steps, a script, notebook?)
  • Print the package versions from the python process that is erroring (to rule out an env mixup)
  • The complete traceback, not just the last-line error

Note that CDS conversion from pandas is something that is maintained continuously under test, in several hundred tests across 15 test jobs, multiple times every day. So offhand I’d say it is unlikely there is any issue with Bokeh itself. Rather there is probably something going on on your end, which is why more information is needed.

@Bryan , i have to apologise for wasting your time on this.

I’ve recreated a new environment and pip installed the relevant packages and it works now.

I was doing this on a jupyter notebook and the cause of this might be due to the kernel not being updated.

1 Like