Skip to content

BUG: Pie chart with categorical values fails #41872

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

Closed
2 of 3 tasks
darkdragon-001 opened this issue Jun 8, 2021 · 1 comment
Closed
2 of 3 tasks

BUG: Pie chart with categorical values fails #41872

darkdragon-001 opened this issue Jun 8, 2021 · 1 comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@darkdragon-001
Copy link

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandas.
  • (optional) I have confirmed this bug exists on the master branch of pandas.

Code Sample, a copy-pastable example

import pandas as pd
pd.Series(pd.Categorical(['cat'], categories=['cat'], ordered=True)).plot.pie()

Problem description

It should be possible to plot a pie chart with categorical values.

Traceback:

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python3/dist-packages/pandas/plotting/_core.py", line 1528, in pie
    return self(kind="pie", **kwargs)
  File "/usr/lib/python3/dist-packages/pandas/plotting/_core.py", line 949, in __call__
    return plot_backend.plot(data, kind=kind, **kwargs)
  File "/usr/lib/python3/dist-packages/pandas/plotting/_matplotlib/__init__.py", line 60, in plot
    plot_obj = PLOT_CLASSES[kind](data, **kwargs)
  File "/usr/lib/python3/dist-packages/pandas/plotting/_matplotlib/core.py", line 1506, in __init__
    data = data.fillna(value=0)
  File "/usr/lib/python3/dist-packages/pandas/core/series.py", line 4530, in fillna
    return super().fillna(
  File "/usr/lib/python3/dist-packages/pandas/core/generic.py", line 6061, in fillna
    new_data = self._mgr.fillna(
  File "/usr/lib/python3/dist-packages/pandas/core/internals/managers.py", line 594, in fillna
    return self.apply(
  File "/usr/lib/python3/dist-packages/pandas/core/internals/managers.py", line 409, in apply
    applied = getattr(b, f)(**kwargs)
  File "/usr/lib/python3/dist-packages/pandas/core/internals/blocks.py", line 1779, in fillna
    values = values.fillna(value=value, limit=limit)
  File "/usr/lib/python3/dist-packages/pandas/core/arrays/categorical.py", line 1721, in fillna
    raise ValueError("fill value must be in categories")
ValueError: fill value must be in categories

Expected Output

It should generate pie chart without errors.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : b5958ee python : 3.9.5.final.0 python-bits : 64 OS : Linux OS-release : 5.8.0-50-generic Version : #56-Ubuntu SMP Mon Apr 12 17:18:36 UTC 2021 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.1.5
numpy : 1.19.5
pytz : 2021.1
dateutil : 2.8.1
pip : 20.3.4
setuptools : 52.0.0
Cython : 0.29.21
pytest : 6.0.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.2.1
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.2
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : None
xlrd : None
xlwt : 1.3.0
numba : None

Additional context

Might be linked to #39320 and #32414

@darkdragon-001 darkdragon-001 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 8, 2021
@darkdragon-001
Copy link
Author

I was using it wrongly. Correct way:

import pandas as pd
pd.Categorical(['cat'], categories=['cat'], ordered=True).value_counts().plot.pie()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant