Skip to content

REGR: Bar plot from Series with IntervalIndex fails in pandas 1.2.0 #38969

Closed
@venaturum

Description

@venaturum
  • 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.


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

Code Sample, a copy-pastable example

import pandas as pd

pd.Series(
    [1,2],
    index=pd.interval_range(0,2)
).plot.bar()

Problem description

The following error is raised in pandas 1.2.0
TypeError: Cannot cast IntervalIndex to dtype int32

pandas 1.1.5 does not have the same issue

stack trace:


TypeError Traceback (most recent call last)
c:\users\rclement\documents.venv\lib\site-packages\pandas\core\arrays\interval.py in astype(self, dtype, copy)
777 try:
--> 778 return np.asarray(self).astype(dtype, copy=copy)
779 except (TypeError, ValueError) as err:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'pandas._libs.interval.Interval'

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

TypeError Traceback (most recent call last)
in
1 pd.Series(
2 [1,2],
----> 3 index=pd.interval_range(0,2)
4 ).plot.bar()

c:\users\rclement\documents.venv\lib\site-packages\pandas\plotting_core.py in bar(self, x, y, **kwargs)
1111 other axis represents a measured value.
1112 """
-> 1113 return self(kind="bar", x=x, y=y, **kwargs)
1114
1115 @appender(

c:\users\rclement\documents.venv\lib\site-packages\pandas\plotting_core.py in call(self, *args, **kwargs)
953 data.columns = label_name
954
--> 955 return plot_backend.plot(data, kind=kind, **kwargs)
956
957 call.doc = doc

c:\users\rclement\documents.venv\lib\site-packages\pandas\plotting_matplotlib_init_.py in plot(data, kind, **kwargs)
59 kwargs["ax"] = getattr(ax, "left_ax", ax)
60 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 61 plot_obj.generate()
62 plot_obj.draw()
63 return plot_obj.result

c:\users\rclement\documents.venv\lib\site-packages\pandas\plotting_matplotlib\core.py in generate(self)
278 self._compute_plot_data()
279 self._setup_subplots()
--> 280 self._make_plot()
281 self._add_table()
282 self._make_legend()

c:\users\rclement\documents.venv\lib\site-packages\pandas\plotting_matplotlib\core.py in _make_plot(self)
1432 if self.orientation == "vertical":
1433 ax.xaxis.update_units(self.ax_index)
-> 1434 self.tick_pos = ax.convert_xunits(self.ax_index).astype(np.int)
1435 elif self.orientation == "horizontal":
1436 ax.yaxis.update_units(self.ax_index)

c:\users\rclement\documents.venv\lib\site-packages\pandas\core\indexes\interval.py in astype(self, dtype, copy)
368 def astype(self, dtype, copy: bool = True):
369 with rewrite_exception("IntervalArray", type(self).name):
--> 370 new_values = self._values.astype(dtype, copy=copy)
371 return Index(new_values, dtype=new_values.dtype, name=self.name)
372

c:\users\rclement\documents.venv\lib\site-packages\pandas\core\arrays\interval.py in astype(self, dtype, copy)
779 except (TypeError, ValueError) as err:
780 msg = f"Cannot cast {type(self).name} to dtype {dtype}"
--> 781 raise TypeError(msg) from err
782
783 def equals(self, other) -> bool:

TypeError: Cannot cast IntervalIndex to dtype int32

Expected Output

A bar plot (produced with pandas 1.1.5):
barplot

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.7.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 1.2.0
numpy : 1.19.4
pytz : 2020.5
dateutil : 2.8.1
pip : 19.2.3
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
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 : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Labels

Needs TestsUnit test(s) needed to prevent regressionsRegressionFunctionality that used to work in a prior pandas versionVisualizationplottinggood first issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions