REGR: Barplot broken on Index(dtype='object') · Issue #38947 · pandas-dev/pandas (original) (raw)

Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

df = pd.DataFrame( { 'a': [1, 2], }, index=pd.Index([0, 'Total']) ) df.plot.bar()

Problem description

On pandas 1.2.0, this raises:

Traceback (most recent call last):
  File "/home/raillard/WFH/LISA/.lisa-venv-3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 706, in astype
    casted = self._values.astype(dtype, copy=copy)
ValueError: invalid literal for int() with base 10: 'Total'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "testplot.py", line 21, in <module>
    df.plot.bar()
  File "/home/raillard/WFH/LISA/.lisa-venv-3.8/lib/python3.8/site-packages/pandas/plotting/_core.py", line 1113, in bar
    return self(kind="bar", x=x, y=y, **kwargs)
  File "/home/raillard/WFH/LISA/.lisa-venv-3.8/lib/python3.8/site-packages/pandas/plotting/_core.py", line 955, in __call__
    return plot_backend.plot(data, kind=kind, **kwargs)
  File "/home/raillard/WFH/LISA/.lisa-venv-3.8/lib/python3.8/site-packages/pandas/plotting/_matplotlib/__init__.py", line 61, in plot
    plot_obj.generate()
  File "/home/raillard/WFH/LISA/.lisa-venv-3.8/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 280, in generate
    self._make_plot()
  File "/home/raillard/WFH/LISA/.lisa-venv-3.8/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 1434, in _make_plot
    self.tick_pos = ax.convert_xunits(self.ax_index).astype(np.int)
  File "/home/raillard/WFH/LISA/.lisa-venv-3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 708, in astype
    raise TypeError(
TypeError: Cannot cast Index to dtype int64

Expected Output

On pandas 1.1.5, it works without issues and produces a plot with 2 bars, one labeled "0" and the other one "Total" as expected.

Output of pd.show_versions()

Python 3.8.7 (default, Dec 24 2020, 17:53:09)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

import pandas as pd
pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.8.7.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.3-arch1-1
Version : #1 SMP PREEMPT Sun, 27 Dec 2020 10:50:46 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : fr_FR.UTF-8

pandas : 1.2.0
numpy : 1.19.4
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.1.1
Cython : None
pytest : 6.2.1
hypothesis : None
sphinx : 3.4.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None