Panda dataframe via numpy to ColumnDataSource problem

Hello,

I am new to Python, Bokeh and coding in general so please forgive me if I have done something dumb…

I have created a dataframe of zeroes using numpy and then I’m trying to pass it into a ColumnDataSource object for manipulation in Bokeh. The fact it is zeroes is not important; any time I try and do this using a dataframe created via a numpy array, I get the same error:

import pandas as pd

import numpy as np

from bokeh.models import ColumnDataSource

df = pd.DataFrame(np.zeros((8,8)))

source = ColumnDataSource(df)

The final line throws this error:

ValueError: expected an element of ColumnData(String, Seq(Any)), got {‘index’: array([0, 1, 2, 3… (it then prints out the entire dataframe)

df looks as expected:

0
1
2
3
4
5
6
7
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

df.info() gives me this, which also seems in order:

<class ‘pandas.core.frame.DataFrame’>

RangeIndex: 8 entries, 0 to 7

Data columns (total 8 columns):

0 8 non-null float64

1 8 non-null float64

2 8 non-null float64

3 8 non-null float64

4 8 non-null float64

5 8 non-null float64

6 8 non-null float64

7 8 non-null float64

dtypes: float64(8)

memory usage: 592.0 bytes

I’m using:

Pandas version: 0.24.1

Bokeh version: 1.0.4

Numpy version: 1.16.1

Thanks,

Sam

An update…

I still get the error if doing:

df = pd.Dataframe(0, index=range(8), colums=(8))

Sam

···

On Wednesday, March 20, 2019 at 2:54:58 PM UTC, Sam Farrington wrote:

Hello,

I am new to Python, Bokeh and coding in general so please forgive me if I have done something dumb…

I have created a dataframe of zeroes using numpy and then I’m trying to pass it into a ColumnDataSource object for manipulation in Bokeh. The fact it is zeroes is not important; any time I try and do this using a dataframe created via a numpy array, I get the same error:

import pandas as pd

import numpy as np

from bokeh.models import ColumnDataSource

df = pd.DataFrame(np.zeros((8,8)))

source = ColumnDataSource(df)

The final line throws this error:

ValueError: expected an element of ColumnData(String, Seq(Any)), got {‘index’: array([0, 1, 2, 3… (it then prints out the entire dataframe)

df looks as expected:

0
1
2
3
4
5
6
7
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

df.info() gives me this, which also seems in order:

<class ‘pandas.core.frame.DataFrame’>

RangeIndex: 8 entries, 0 to 7

Data columns (total 8 columns):

0 8 non-null float64

1 8 non-null float64

2 8 non-null float64

3 8 non-null float64

4 8 non-null float64

5 8 non-null float64

6 8 non-null float64

7 8 non-null float64

dtypes: float64(8)

memory usage: 592.0 bytes

I’m using:

Pandas version: 0.24.1

Bokeh version: 1.0.4

Numpy version: 1.16.1

Thanks,

Sam

It seems like it has to do with the columns on the dataframe. I was able to get your little bit of code above working by specifying column names:

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

``

It will NOT work if the column headings are integer types (which I think is what you’re getting by default here). You could avoid explicitly naming your columns by generating a list of ints and converting:

df = pd.DataFrame(data=np.zeros((8,8)), columns=[str(i) for i in np.arange(0, 8)])

``

Try it out.

···

On Wednesday, March 20, 2019 at 9:54:58 AM UTC-5, Sam Farrington wrote:

Hello,

I am new to Python, Bokeh and coding in general so please forgive me if I have done something dumb…

I have created a dataframe of zeroes using numpy and then I’m trying to pass it into a ColumnDataSource object for manipulation in Bokeh. The fact it is zeroes is not important; any time I try and do this using a dataframe created via a numpy array, I get the same error:

import pandas as pd

import numpy as np

from bokeh.models import ColumnDataSource

df = pd.DataFrame(np.zeros((8,8)))

source = ColumnDataSource(df)

The final line throws this error:

ValueError: expected an element of ColumnData(String, Seq(Any)), got {‘index’: array([0, 1, 2, 3… (it then prints out the entire dataframe)

df looks as expected:

0
1
2
3
4
5
6
7
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

df.info() gives me this, which also seems in order:

<class ‘pandas.core.frame.DataFrame’>

RangeIndex: 8 entries, 0 to 7

Data columns (total 8 columns):

0 8 non-null float64

1 8 non-null float64

2 8 non-null float64

3 8 non-null float64

4 8 non-null float64

5 8 non-null float64

6 8 non-null float64

7 8 non-null float64

dtypes: float64(8)

memory usage: 592.0 bytes

I’m using:

Pandas version: 0.24.1

Bokeh version: 1.0.4

Numpy version: 1.16.1

Thanks,

Sam

Hi.

This is exactly correct. Just to state it in other terms: A CDS maps *string* column names to lists/arrays/series of data. It is not possible to have non-string column names (in fact I am surprised to know Pandas allows this).

Thanks,

Bryan

···

On Mar 20, 2019, at 5:30 AM, Sam <[email protected]> wrote:

Hello,

I am new to Python, Bokeh and coding in general so please forgive me if I have done something dumb...

I have created a dataframe of zeroes using numpy and then I'm trying to pass it into a ColumnDataSource object for manipulation in Bokeh. The fact it is zeroes is not important; any time I try and do this using a dataframe created via a numpy array, I get the same error:

import pandas as pd
import numpy as np

from bokeh.models import ColumnDataSource

df = pd.DataFrame(np.zeros((8,8)))

source = ColumnDataSource(df)

The final line throws this error:

ValueError: expected an element of ColumnData(String, Seq(Any)), got {'index': array([0, 1, 2, 3... (it then prints out the entire dataframe)

df looks as expected:

0 1 2 3 4 5 6 7
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

df.info() gives me this, which also seems in order:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8 entries, 0 to 7
Data columns (total 8 columns):
0 8 non-null float64
1 8 non-null float64
2 8 non-null float64
3 8 non-null float64
4 8 non-null float64
5 8 non-null float64
6 8 non-null float64
7 8 non-null float64
dtypes: float64(8)
memory usage: 592.0 bytes

I'm using:
Pandas version: 0.24.1
Bokeh version: 1.0.4
Numpy version: 1.16.1

Thanks,
Sam

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/cfb497c1-d1a4-40cf-852a-7bce5deaf565%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Ah, makes sense. Thanks a lot, works perfectly :slight_smile:

···

On Wednesday, March 20, 2019 at 10:34:51 PM UTC, [email protected] wrote:

It seems like it has to do with the columns on the dataframe. I was able to get your little bit of code above working by specifying column names:

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

``

It will NOT work if the column headings are integer types (which I think is what you’re getting by default here). You could avoid explicitly naming your columns by generating a list of ints and converting:

df = pd.DataFrame(data=np.zeros((8,8)), columns=[str(i) for i in np.arange(0, 8)])

``

Try it out.

On Wednesday, March 20, 2019 at 9:54:58 AM UTC-5, Sam Farrington wrote:

Hello,

I am new to Python, Bokeh and coding in general so please forgive me if I have done something dumb…

I have created a dataframe of zeroes using numpy and then I’m trying to pass it into a ColumnDataSource object for manipulation in Bokeh. The fact it is zeroes is not important; any time I try and do this using a dataframe created via a numpy array, I get the same error:

import pandas as pd

import numpy as np

from bokeh.models import ColumnDataSource

df = pd.DataFrame(np.zeros((8,8)))

source = ColumnDataSource(df)

The final line throws this error:

ValueError: expected an element of ColumnData(String, Seq(Any)), got {‘index’: array([0, 1, 2, 3… (it then prints out the entire dataframe)

df looks as expected:

0
1
2
3
4
5
6
7
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

df.info() gives me this, which also seems in order:

<class ‘pandas.core.frame.DataFrame’>

RangeIndex: 8 entries, 0 to 7

Data columns (total 8 columns):

0 8 non-null float64

1 8 non-null float64

2 8 non-null float64

3 8 non-null float64

4 8 non-null float64

5 8 non-null float64

6 8 non-null float64

7 8 non-null float64

dtypes: float64(8)

memory usage: 592.0 bytes

I’m using:

Pandas version: 0.24.1

Bokeh version: 1.0.4

Numpy version: 1.16.1

Thanks,

Sam