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

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

Closed
3 tasks done
venaturum opened this issue Jan 5, 2021 · 16 comments · Fixed by #47344
Closed
3 tasks done

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

venaturum opened this issue Jan 5, 2021 · 16 comments · Fixed by #47344
Assignees
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions Regression Functionality that used to work in a prior pandas version Visualization plotting

Comments

@venaturum
Copy link
Contributor

venaturum commented Jan 5, 2021

  • 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

@venaturum venaturum added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 5, 2021
@MarcoGorelli
Copy link
Member

MarcoGorelli commented Jan 5, 2021

Thanks @venaturum for the report - could you put a descriptive title too please? Also, show the full traceback, not just the final part with the error message?

@MarcoGorelli MarcoGorelli added Visualization plotting Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member Bug labels Jan 5, 2021
@jorisvandenbossche
Copy link
Member

Didn't check, but given the other reports, I suppose this is similarly as #38947, #38865, #38736 caused by #28733

@jorisvandenbossche jorisvandenbossche added this to the 1.2.1 milestone Jan 5, 2021
@venaturum venaturum changed the title BUG: BUG: Bar plot from Series with IntervalIndex fails in pandas 1.2.0 Jan 5, 2021
@venaturum
Copy link
Contributor Author

@MarcoGorelli , done now, sorry about that!

@jorisvandenbossche, I think you're right, looks especially similar to 38947.

@simonjayhawkins
Copy link
Member

@jorisvandenbossche, I think you're right, looks especially similar to 38947.

both these fail from self.tick_pos = ax.convert_xunits(self.ax_index).astype(np.int) in _make_plot in pandas\plotting\_matplotlib\core.py

could close as duplicate or leave open to ensure additional test coverage

@simonjayhawkins simonjayhawkins added the Closing Candidate May be closeable, needs more eyeballs label Jan 6, 2021
@simonjayhawkins simonjayhawkins changed the title BUG: Bar plot from Series with IntervalIndex fails in pandas 1.2.0 REGR: Bar plot from Series with IntervalIndex fails in pandas 1.2.0 Jan 6, 2021
@jorisvandenbossche
Copy link
Member

(let's keep open until we actually fix + add a test for this specific case)

@jorisvandenbossche jorisvandenbossche removed the Closing Candidate May be closeable, needs more eyeballs label Jan 15, 2021
@venaturum
Copy link
Contributor Author

No problem, I thought the closing candidate tag might have been a subtle hint 😂

@simonjayhawkins simonjayhawkins added the Needs Tests Unit test(s) needed to prevent regressions label Jan 18, 2021
@simonjayhawkins
Copy link
Member

Didn't check, but given the other reports, I suppose this is similarly as #38947, #38865, #38736 caused by #28733

#28733 has been reverted. needs test to prevent regression.

@simonjayhawkins simonjayhawkins modified the milestones: 1.2.1, Contributions Welcome Jan 18, 2021
@mariana-LJ
Copy link
Contributor

Could anyone please clarify what's needed here? Are we only missing a regression test? I'd be happy to help with this issue.

@MarcoGorelli
Copy link
Member

Yes, that's right

@mariana-LJ
Copy link
Contributor

Thank you @MarcoGorelli! In that case, I'll take this issue and start to work on it as soon as I can.

@mariana-LJ
Copy link
Contributor

To ensure this issue is assigned to me: take.

@mariana-LJ
Copy link
Contributor

@simonjayhawkins, @mroeschke, @MarcoGorelli: I left a comment above with the keyword 'take' but this issue was not assigned to me. Am I missing some step? Could any of you please assign it to me? Thanks!

@MarcoGorelli
Copy link
Member

the comment should only have a single word: take

if your comment includes other words, it won't work

@mariana-LJ
Copy link
Contributor

I see. Thanks!

@mariana-LJ
Copy link
Contributor

take

@noatamir
Copy link
Member

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions Regression Functionality that used to work in a prior pandas version Visualization plotting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants