Skip to content

BUG: df.plot.barh() broken from 1.1.5 to 1.2 under specific circumstances #39126

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
LarsWigger opened this issue Jan 12, 2021 · 6 comments · Fixed by #47306
Closed
2 of 3 tasks

BUG: df.plot.barh() broken from 1.1.5 to 1.2 under specific circumstances #39126

LarsWigger opened this issue Jan 12, 2021 · 6 comments · Fixed by #47306
Labels
Duplicate Report Duplicate issue or pull request 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

@LarsWigger
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
df_dict_list = [{"word": 1, "value": 0}, {"word": "knowledg", "value": 2}]
df = pd.DataFrame(df_dict_list)
df.plot.barh(x="word", legend=None)

(executed as part of a notebook, but this should not matter)

Problem description

The code works in Pandas 1.1.5, but not in 1.2. I suppose that this is not intentional. Additionally, the bug seems to occur only when the first value is an integer and a following value cannot be parsed to an int. This probably goes rather deep into the library, so I cannot say whether barh is the actual problem or whether it lies somewhere else.

Stack trace

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/.conda/envs/BibTex/lib/python3.8/site-packages/pandas/core/indexes/base.py in astype(self, dtype, copy)
    705         try:
--> 706             casted = self._values.astype(dtype, copy=copy)
    707         except (TypeError, ValueError) as err:

ValueError: invalid literal for int() with base 10: 'knowledg'

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

TypeError                                 Traceback (most recent call last)
<ipython-input-3-3b2bde8f17ab> in <module>
----> 1 df.plot.barh(x="word", legend=None)

~/.conda/envs/BibTex/lib/python3.8/site-packages/pandas/plotting/_core.py in barh(self, x, y, **kwargs)
   1197         other axis represents a measured value.
   1198         """
-> 1199         return self(kind="barh", x=x, y=y, **kwargs)
   1200 
   1201     def box(self, by=None, **kwargs):

~/.conda/envs/BibTex/lib/python3.8/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__

~/.conda/envs/BibTex/lib/python3.8/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

~/.conda/envs/BibTex/lib/python3.8/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()

~/.conda/envs/BibTex/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py in _make_plot(self)
   1435             elif self.orientation == "horizontal":
   1436                 ax.yaxis.update_units(self.ax_index)
-> 1437                 self.tick_pos = ax.convert_yunits(self.ax_index).astype(np.int)
   1438             self.ax_pos = self.tick_pos - self.tickoffset
   1439 

~/.conda/envs/BibTex/lib/python3.8/site-packages/pandas/core/indexes/base.py in astype(self, dtype, copy)
    706             casted = self._values.astype(dtype, copy=copy)
    707         except (TypeError, ValueError) as err:
--> 708             raise TypeError(
    709                 f"Cannot cast {type(self).__name__} to dtype {dtype}"
    710             ) from err

TypeError: Cannot cast Index to dtype int64
@LarsWigger LarsWigger added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 12, 2021
@phofl
Copy link
Member

phofl commented Jan 12, 2021

Hi, thanks for your report. This is a duplicate of #38947

@phofl phofl added Duplicate Report Duplicate issue or pull request Regression Functionality that used to work in a prior pandas version Visualization plotting and removed Needs Triage Issue that has not been reviewed by a pandas team member Bug labels Jan 12, 2021
@LarsWigger LarsWigger changed the title BUG: df.plot.bar() broken from 1.1.5 to 1.2 under specific circumstances BUG: df.plot.barh() broken from 1.1.5 to 1.2 under specific circumstances Jan 12, 2021
@simonjayhawkins simonjayhawkins added this to the 1.2.1 milestone Jan 14, 2021
@simonjayhawkins simonjayhawkins added the Needs Tests Unit test(s) needed to prevent regressions label Jan 18, 2021
@simonjayhawkins
Copy link
Member

Hi, thanks for your report. This is a duplicate of #38947

The commit that caused the regression has been reverted. needs tests to prevent regressions

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

@simonjayhawkins can I add test cases for this?

@thaisbendixen
Copy link

Can someone please assign this issue to me?

@mariana-LJ
Copy link
Contributor

Can someone please assign this issue to me?

@thaisbendixen: You can assign this issue to yourself. Here are the instructions to do so (fourth paragraph): https://pandas.pydata.org/docs/development/contributing.html#where-to-start

@dataxerik
Copy link
Contributor

Hi,

I want to pick this up, but just wanted to make sure it was still relevant and that I understand the requirements. We just want to have a test to help avoid this issue going forward? It seems like #46451 addressed the test issue.

It uses bar rather than barh, but it uses an integer-string index combination which seems to be main issue.

So, are there additional test we want or is this good to close?

mroeschke added a commit that referenced this issue Jun 10, 2022
#47306)

* TST: barh plot with string and integer at the same column (#39126)

* Update pandas/tests/plotting/test_misc.py

Co-authored-by: Matthew Roeschke <[email protected]>
yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this issue Jul 13, 2022
pandas-dev#47306)

* TST: barh plot with string and integer at the same column (pandas-dev#39126)

* Update pandas/tests/plotting/test_misc.py

Co-authored-by: Matthew Roeschke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request 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.

8 participants