Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't make bar chart after groupby. #12

Closed
fx-kirin opened this Issue Feb 27, 2019 · 3 comments

Comments

Projects
None yet
2 participants
@fx-kirin
Copy link

fx-kirin commented Feb 27, 2019

Thank you very much for making great package. I'm really into it. I'd like to report the bug with the latest version

Environment

pandas 0.23.3
pandas-bokeh 0.1.1
Python 3.6

The program

import numpy as np
import pandas as pd
import pandas_bokeh
pandas_bokeh.output_file("Interactive Plot.html")

x = np.arange(0, 100, 0.1)
df = pd.DataFrame(x)
result = df.groupby(pd.cut(df[0], np.arange(0, 100, 10))).sum()
result.plot_bokeh.bar()

Error

$ python '/tmp/tmp.GaLHNeE1Eg/test.py'
Traceback (most recent call last):
  File "/tmp/tmp.GaLHNeE1Eg/test.py", line 20, in <module>
    result.plot_bokeh.bar()
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/pandas_bokeh/plot.py", line 1845, in bar
    return self(kind="bar", x=x, y=y, **kwds)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/pandas_bokeh/plot.py", line 1659, in __call__
    return plot(self._data, *args, **kwargs)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/pandas_bokeh/plot.py", line 879, in plot
    show(p)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/io/showing.py", line 137, in show
    return _show_with_state(obj, state, browser, new, notebook_handle=notebook_handle)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/io/showing.py", line 175, in _show_with_state
    _show_file_with_state(obj, state, new, controller)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/io/showing.py", line 158, in _show_file_with_state
    filename = save(obj, state=state)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/io/saving.py", line 83, in save
    _save_helper(obj, filename, resources, title, template)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/io/saving.py", line 145, in _save_helper
    html = file_html(obj, resources, title=title, template=template)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/embed/standalone.py", line 281, in file_html
    (docs_json, render_items) = standalone_docs_json_and_render_items(models)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/embed/util.py", line 340, in standalone_docs_json_and_render_items
    docs_json[docid] = doc.to_json()
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/document/document.py", line 781, in to_json
    doc_json = self.to_json_string()
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/document/document.py", line 810, in to_json_string
    return serialize_json(json, indent=indent)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/core/json_encoder.py", line 213, in serialize_json
    return json.dumps(obj, cls=BokehJSONEncoder, allow_nan=False, indent=indent, separators=separators, sort_keys=True, **kwargs)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/core/json_encoder.py", line 135, in default
    return self.transform_python_types(obj)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/site-packages/bokeh/core/json_encoder.py", line 102, in transform_python_types
    return super(BokehJSONEncoder, self).default(obj)
  File "/home/myuser/.pyenv/versions/miniconda3-4.1.11/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'Interval' is not JSON serializable

I think it happens because groupy method makes categorical index.

@PatrikHlobil

This comment has been minimized.

Copy link
Owner

PatrikHlobil commented Feb 27, 2019

Hi, I'm glad you enjoy using my package and thanks for reporting me your bug.

The reason why it fails is the following, when I look at your result DataFrame, it looks like:

image

So, the values of the index are tuples (or better Pandas Intervals). The bar method then basically takes the index (or what should be used for the x-axis) and converts it to a string to show it on the x-axis. (so basically each bar has a central point). However, it also stores the original object in the plot (for using it as a DataSource). Since Bokeh translates everything into a finalo html, it tries to serialize everything to JSON, but he doesn't know how to do this for a Pandas interval.

There are now two possible solutions for your problem. Either you make:

import numpy as np
import pandas as pd
import pandas_bokeh
pandas_bokeh.output_notebook()

x = np.arange(0, 100, 0.1)
df = pd.DataFrame(x)
result = df.groupby(pd.cut(df[0], np.arange(0, 100, 10))).sum()
result.index = result.index.astype(str)
result.plot_bokeh.bar()

image

Alternatively, I would propose that you do:

import numpy as np
import pandas as pd
import pandas_bokeh
pandas_bokeh.output_notebook()

x = np.arange(0, 100, 0.1)
df = pd.DataFrame(x)
result = df.groupby(pd.cut(df[0], np.arange(0, 100, 10))).sum()
result.index = np.arange(5, 90, 10)
result.plot_bokeh.bar()

image

I hope that I could help you. If you have further questions, please answer to this post. If you issue is solved, please also write me such that I can close it.

Cheers, Patrik

@PatrikHlobil PatrikHlobil added the bug label Feb 27, 2019

@fx-kirin

This comment has been minimized.

Copy link
Author

fx-kirin commented Feb 28, 2019

.astype(str) method looks good to me. Thank very much for the kind response and showing the work around!

@PatrikHlobil

This comment has been minimized.

Copy link
Owner

PatrikHlobil commented Feb 28, 2019

You’re welcome. Maybe you also want to give the histogram plot a chance ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.