Skip to content
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

DataFrameGroupby.boxplot fails when subplots=False #28102

Merged
merged 30 commits into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7e461a1
remove \n from docstring
charlesdong1991 Dec 3, 2018
1314059
fix conflicts
charlesdong1991 Jan 19, 2019
8bcb313
Merge remote-tracking branch 'upstream/master'
charlesdong1991 Jul 30, 2019
a30fd5c
Merge remote-tracking branch 'upstream/master' into fix_issue_16748
charlesdong1991 Aug 22, 2019
1d0ac65
Fix issue 16748
charlesdong1991 Aug 22, 2019
af41084
Code change based on review
charlesdong1991 Aug 23, 2019
193eb2c
Fix import sort linting
charlesdong1991 Aug 23, 2019
db214b6
Merge remote-tracking branch 'upstream/master' into fix_issue_16748
charlesdong1991 Aug 23, 2019
dfc72b2
Skip the failing test
charlesdong1991 Aug 24, 2019
6cd2d28
Merge remote-tracking branch 'upstream/master' into fix_issue_16748
charlesdong1991 Aug 27, 2019
5c69d10
Remove skip
charlesdong1991 Aug 30, 2019
c08c278
remove imports
charlesdong1991 Aug 30, 2019
1df91da
More careful change
charlesdong1991 Aug 31, 2019
24c5d93
fix conflict
charlesdong1991 Sep 5, 2019
9cce7f7
keep the change
charlesdong1991 Sep 9, 2019
0df3670
fix conflict and merge master
charlesdong1991 Oct 19, 2019
2f99b67
update solution
charlesdong1991 Oct 19, 2019
3819f85
update test
charlesdong1991 Oct 19, 2019
3cbba0f
fix test
charlesdong1991 Oct 20, 2019
51a326f
Merge remote-tracking branch 'upstream/master' into fix_issue_16748
charlesdong1991 Dec 28, 2019
011218b
Merge remote-tracking branch 'upstream/master' into fix_issue_16748
charlesdong1991 Dec 30, 2019
c786a55
much better solution
charlesdong1991 Oct 3, 2020
a1d84b9
format
charlesdong1991 Oct 3, 2020
4eecae8
typo
charlesdong1991 Oct 3, 2020
b6d1b4c
whatsnew
charlesdong1991 Oct 3, 2020
8ab2db3
commit one more
charlesdong1991 Oct 3, 2020
9f5caaa
Merge remote-tracking branch 'upstream/master' into fix_issue_16748
charlesdong1991 Oct 13, 2020
6bf3914
Merge remote-tracking branch 'upstream/master' into fix_issue_16748
charlesdong1991 Oct 26, 2020
a2884e7
Merge remote-tracking branch 'upstream/master' into fix_issue_16748
charlesdong1991 Oct 29, 2020
067323d
Merge remote-tracking branch 'upstream/master' into fix_issue_16748
charlesdong1991 Nov 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Plotting
- Bug in :meth:`DataFrame.plot` producing incorrect legend markers when plotting multiple series on the same axis (:issue:`18222`)
- Bug in :meth:`DataFrame.plot` when ``kind='box'`` and data contains datetime or timedelta data. These types are now automatically dropped (:issue:`22799`)
- Bug in :meth:`DataFrame.plot.line` and :meth:`DataFrame.plot.area` produce wrong xlim in x-axis (:issue:`27686`, :issue:`25160`, :issue:`24784`)
- Bug in :meth:`DataFrameGroupby.boxplot` when ``subplots=False``, a KeyError would raise (:issue:`16748`)
gfyoung marked this conversation as resolved.
Show resolved Hide resolved

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
16 changes: 14 additions & 2 deletions pandas/plotting/_matplotlib/boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pandas as pd

from pandas import IndexSlice, Index, MultiIndex
from pandas.io.formats.printing import pprint_thing
from pandas.plotting._matplotlib import converter
from pandas.plotting._matplotlib.core import LinePlot, MPLPlot
Expand Down Expand Up @@ -314,10 +315,21 @@ def plot_group(keys, values, ax):
with plt.rc_context(rc):
ax = plt.gca()
data = data._get_numeric_data()

# if columns is None, use all numeric columns of data; if data columns
# is multiIndex, which means a groupby has been applied before, select
gfyoung marked this conversation as resolved.
Show resolved Hide resolved
# data using new grouped column names; if data columns is Index, select
# data simply using columns
if columns is None:
columns = data.columns
else:
data = data[columns]
elif isinstance(data.columns, MultiIndex):

# reselect columns with after-groupby multi-index columns
data = data.loc[:, IndexSlice[:, columns]]
columns = data.columns
elif isinstance(data.columns, Index):
data = data.loc[:, columns]
columns = data.columns

result = plot_group(columns, data.values.T, ax)
ax.grid(grid)
Expand Down
43 changes: 43 additions & 0 deletions pandas/tests/plotting/test_boxplot_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,46 @@ def test_fontsize(self):
self._check_ticks_props(
df.boxplot("a", by="b", fontsize=16), xlabelsize=16, ylabelsize=16
)

@pytest.mark.parametrize(
"col, expected_xticklabel",
[
("v", ["(a, v)", "(b, v)", "(c, v)", "(d, v)", "(e, v)"]),
(["v"], ["(a, v)", "(b, v)", "(c, v)", "(d, v)", "(e, v)"]),
("v1", ["(a, v1)", "(b, v1)", "(c, v1)", "(d, v1)", "(e, v1)"]),
(
["v", "v1"],
[
"(a, v)",
"(a, v1)",
"(b, v)",
"(b, v1)",
"(c, v)",
"(c, v1)",
"(d, v)",
"(d, v1)",
"(e, v)",
"(e, v1)",
],
),
],
)
def test_groupby_boxplot_subplots_false(self, col, expected_xticklabel):
# GH 16748
df = DataFrame(
{
"cat": np.random.choice(list("abcde"), 100),
"v": np.random.rand(100),
"v1": np.random.rand(100),
}
)
grouped = df.groupby("cat")

# check is boxplot with subplots=False works
charlesdong1991 marked this conversation as resolved.
Show resolved Hide resolved
axes = _check_plot_works(
grouped.boxplot, subplots=False, column=col, return_type="axes"
)

# check if xticks labels are plotted correctly
result_xticklabel = [x.get_text() for x in axes.get_xticklabels()]
assert expected_xticklabel == result_xticklabel