Skip to content

Commit 6afa2ad

Browse files
charlesdong1991TomAugspurger
authored andcommitted
BUG: Allow plotting boolean values (#27665)
1 parent 01f90c1 commit 6afa2ad

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ I/O
163163
Plotting
164164
^^^^^^^^
165165

166-
-
166+
- Bug in :meth:`Series.plot` not able to plot boolean values (:issue:`23719`)
167167
-
168168

169169
Groupby/resample/rolling

pandas/plotting/_core.py

+2
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ class PlotAccessor(PandasObject):
586586
mark_right : bool, default True
587587
When using a secondary_y axis, automatically mark the column
588588
labels with "(right)" in the legend
589+
include_bool : bool, default is False
590+
If True, boolean values can be plotted
589591
`**kwds` : keywords
590592
Options to pass to matplotlib plotting method
591593

pandas/plotting/_matplotlib/core.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def __init__(
106106
colormap=None,
107107
table=False,
108108
layout=None,
109+
include_bool=False,
109110
**kwds
110111
):
111112

@@ -191,6 +192,7 @@ def __init__(
191192
self.colormap = colormap
192193

193194
self.table = table
195+
self.include_bool = include_bool
194196

195197
self.kwds = kwds
196198

@@ -400,9 +402,12 @@ def _compute_plot_data(self):
400402
# GH16953, _convert is needed as fallback, for ``Series``
401403
# with ``dtype == object``
402404
data = data._convert(datetime=True, timedelta=True)
403-
numeric_data = data.select_dtypes(
404-
include=[np.number, "datetime", "datetimetz", "timedelta"]
405-
)
405+
select_include_type = [np.number, "datetime", "datetimetz", "timedelta"]
406+
407+
# GH23719, allow plotting boolean
408+
if self.include_bool is True:
409+
select_include_type.append(np.bool_)
410+
numeric_data = data.select_dtypes(include=select_include_type)
406411

407412
try:
408413
is_empty = numeric_data.empty

pandas/tests/plotting/test_series.py

+9
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ def test_label(self):
167167
ax.legend() # draw it
168168
self._check_legend_labels(ax, labels=["LABEL"])
169169

170+
def test_boolean(self):
171+
# GH 23719
172+
s = Series([False, False, True])
173+
_check_plot_works(s.plot, include_bool=True)
174+
175+
msg = "no numeric data to plot"
176+
with pytest.raises(TypeError, match=msg):
177+
_check_plot_works(s.plot)
178+
170179
def test_line_area_nan_series(self):
171180
values = [1, 2, np.nan, 3]
172181
s = Series(values)

0 commit comments

Comments
 (0)