Skip to content

[BUG] Fix DataFrameGroupBy.boxplot with subplots=False fails for object columns #44003

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

Merged
merged 8 commits into from
Nov 14, 2021
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ Period

Plotting
^^^^^^^^
-
- Fixed bug in :meth:`boxplot._grouped_plot_by_column` where trying to plot data of dtype ``object`` caused plotting to fail even if some data was numeric
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the note should be user facing, not referencing an internal function. Include the issue number (see other notes for how to do this)

-

Groupby/resample/rolling
Expand Down
1 change: 1 addition & 0 deletions pandas/plotting/_matplotlib/boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ def plot_group(keys, values, ax: Axes):
rc = {"figure.figsize": figsize} if figsize is not None else {}
with plt.rc_context(rc):
ax = plt.gca()
data = data.apply(pd.to_numeric, errors="ignore")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm why do you think we should do this? if its an object column its not numeric. sure it can be coerced but that's up to the user.

data = data._get_numeric_data()
if columns is None:
columns = data.columns
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/plotting/test_boxplot_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,12 @@ def test_groupby_boxplot_subplots_false(self, col, expected_xticklabel):
result_xticklabel = [x.get_text() for x in axes.get_xticklabels()]
assert expected_xticklabel == result_xticklabel

def test_groupby_boxplot_object(self):
# GH 43480
df = self.hist_df.astype("object")
grouped = df.groupby("gender")
_check_plot_works(grouped.boxplot, subplots=False)

def test_boxplot_multiindex_column(self):
# GH 16748
arrays = [
Expand Down