diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 58918f2d8c40e..974d14a4b424c 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -163,7 +163,7 @@ I/O Plotting ^^^^^^^^ -- +- Bug in :meth:`Series.plot` not able to plot boolean values (:issue:`23719`) - Groupby/resample/rolling diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index a3c1499845c2a..ec5c609c1b267 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -586,6 +586,8 @@ class PlotAccessor(PandasObject): mark_right : bool, default True When using a secondary_y axis, automatically mark the column labels with "(right)" in the legend + include_bool : bool, default is False + If True, boolean values can be plotted `**kwds` : keywords Options to pass to matplotlib plotting method diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index c2b37bb297ecb..50f0d16631a15 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -106,6 +106,7 @@ def __init__( colormap=None, table=False, layout=None, + include_bool=False, **kwds ): @@ -191,6 +192,7 @@ def __init__( self.colormap = colormap self.table = table + self.include_bool = include_bool self.kwds = kwds @@ -400,9 +402,12 @@ def _compute_plot_data(self): # GH16953, _convert is needed as fallback, for ``Series`` # with ``dtype == object`` data = data._convert(datetime=True, timedelta=True) - numeric_data = data.select_dtypes( - include=[np.number, "datetime", "datetimetz", "timedelta"] - ) + select_include_type = [np.number, "datetime", "datetimetz", "timedelta"] + + # GH23719, allow plotting boolean + if self.include_bool is True: + select_include_type.append(np.bool_) + numeric_data = data.select_dtypes(include=select_include_type) try: is_empty = numeric_data.empty diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index 8b4a78e9195b5..111c3a70fc09c 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -167,6 +167,15 @@ def test_label(self): ax.legend() # draw it self._check_legend_labels(ax, labels=["LABEL"]) + def test_boolean(self): + # GH 23719 + s = Series([False, False, True]) + _check_plot_works(s.plot, include_bool=True) + + msg = "no numeric data to plot" + with pytest.raises(TypeError, match=msg): + _check_plot_works(s.plot) + def test_line_area_nan_series(self): values = [1, 2, np.nan, 3] s = Series(values)