Skip to content

Commit 4d69450

Browse files
authored
[BUG] Fix DataFrameGroupBy.boxplot with subplots=False fails for object columns (#44003)
1 parent b1053c4 commit 4d69450

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ Period
646646

647647
Plotting
648648
^^^^^^^^
649-
-
649+
- When given non-numeric data, :meth:`DataFrame.boxplot` now raises a ``ValueError`` rather than a cryptic ``KeyError`` or ``ZeroDivsionError``, in line with other plotting functions like :meth:`DataFrame.hist`. (:issue:`43480`)
650650
-
651651

652652
Groupby/resample/rolling

pandas/plotting/_matplotlib/boxplot.py

+5
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ def plot_group(keys, values, ax: Axes):
391391
with plt.rc_context(rc):
392392
ax = plt.gca()
393393
data = data._get_numeric_data()
394+
naxes = len(data.columns)
395+
if naxes == 0:
396+
raise ValueError(
397+
"boxplot method requires numerical columns, nothing to plot."
398+
)
394399
if columns is None:
395400
columns = data.columns
396401
else:

pandas/tests/plotting/test_boxplot_method.py

+8
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,14 @@ def test_groupby_boxplot_subplots_false(self, col, expected_xticklabel):
543543
result_xticklabel = [x.get_text() for x in axes.get_xticklabels()]
544544
assert expected_xticklabel == result_xticklabel
545545

546+
def test_groupby_boxplot_object(self):
547+
# GH 43480
548+
df = self.hist_df.astype("object")
549+
grouped = df.groupby("gender")
550+
msg = "boxplot method requires numerical columns, nothing to plot"
551+
with pytest.raises(ValueError, match=msg):
552+
_check_plot_works(grouped.boxplot, subplots=False)
553+
546554
def test_boxplot_multiindex_column(self):
547555
# GH 16748
548556
arrays = [

0 commit comments

Comments
 (0)