Skip to content

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

Closed
@LarsWigger

Description

@LarsWigger
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Duplicate ReportDuplicate issue or pull requestNeeds 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