From 97490e7d7ba7e89003f350c17ba0d5208d563361 Mon Sep 17 00:00:00 2001 From: zhuoyahuang97 Date: Sun, 28 Sep 2025 18:32:26 -0400 Subject: [PATCH 1/3] fix boxplot column misorder --- pandas/plotting/_matplotlib/boxplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index 32b1fbe73a310..00646979b3dc2 100644 --- a/pandas/plotting/_matplotlib/boxplot.py +++ b/pandas/plotting/_matplotlib/boxplot.py @@ -214,7 +214,7 @@ def _make_plot(self, fig: Figure) -> None: # values, instead of label which is used as subtitle in this case. # error: "Index" has no attribute "levels"; maybe "nlevels"? levels = self.data.columns.levels # type: ignore[attr-defined] - ticklabels = [pprint_thing(col) for col in levels[0]] + ticklabels = [pprint_thing(col) for col in self.data.columns.get_level_values(0)] # else: ticklabels = [pprint_thing(label)] From 84d2d79850e5b5f7e5b62f25c641a1eb23308f72 Mon Sep 17 00:00:00 2001 From: zhuoyahuang97 Date: Sun, 28 Sep 2025 19:01:00 -0400 Subject: [PATCH 2/3] add test --- pandas/tests/plotting/test_boxplot_method.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/tests/plotting/test_boxplot_method.py b/pandas/tests/plotting/test_boxplot_method.py index 2267b6197cd80..db6872eb6c246 100644 --- a/pandas/tests/plotting/test_boxplot_method.py +++ b/pandas/tests/plotting/test_boxplot_method.py @@ -9,6 +9,7 @@ import pytest from pandas import ( + Categorical, DataFrame, MultiIndex, Series, @@ -408,6 +409,14 @@ def test_boxplot_group_no_xlabel_ylabel(self, vert, request): ) assert target_label == pprint_thing(["group"]) + @pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning") + def test_boxplot_group_xlabel_ylabel(self, vert): + df = DataFrame({'value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + 'label': ['c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'a', 'a']}) + df.label = Categorical(df.label, categories=['c', 'b', 'a'], ordered=True) + ax = df.boxplot(by="label") + xticklabels = ax.get_xticklabels() + assert [x.get_text() for x in xticklabels] == ['c', 'b', 'a'] class TestDataFrameGroupByPlots: def test_boxplot_legacy1(self, hist_df): From 5e30ba6ff3b80b9f54ed00daf078713d05b93625 Mon Sep 17 00:00:00 2001 From: zhuoyahuang97 Date: Sun, 28 Sep 2025 19:03:55 -0400 Subject: [PATCH 3/3] fix failed cicd --- pandas/plotting/_matplotlib/boxplot.py | 7 ++++--- pandas/tests/plotting/test_boxplot_method.py | 15 ++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index 00646979b3dc2..b768a4f623203 100644 --- a/pandas/plotting/_matplotlib/boxplot.py +++ b/pandas/plotting/_matplotlib/boxplot.py @@ -212,9 +212,10 @@ def _make_plot(self, fig: Figure) -> None: # When `by` is assigned, the ticklabels will become unique grouped # values, instead of label which is used as subtitle in this case. - # error: "Index" has no attribute "levels"; maybe "nlevels"? - levels = self.data.columns.levels # type: ignore[attr-defined] - ticklabels = [pprint_thing(col) for col in self.data.columns.get_level_values(0)] # + ticklabels = [ + pprint_thing(col) + for col in self.data.columns.get_level_values(0) + ] else: ticklabels = [pprint_thing(label)] diff --git a/pandas/tests/plotting/test_boxplot_method.py b/pandas/tests/plotting/test_boxplot_method.py index db6872eb6c246..85c509ac1f965 100644 --- a/pandas/tests/plotting/test_boxplot_method.py +++ b/pandas/tests/plotting/test_boxplot_method.py @@ -410,13 +410,18 @@ def test_boxplot_group_no_xlabel_ylabel(self, vert, request): assert target_label == pprint_thing(["group"]) @pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning") - def test_boxplot_group_xlabel_ylabel(self, vert): - df = DataFrame({'value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], - 'label': ['c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'a', 'a']}) - df.label = Categorical(df.label, categories=['c', 'b', 'a'], ordered=True) + def test_boxplot_group_ordered_ticklabel(self, vert): + df = DataFrame( + { + "value": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + "label": ["c", "c", "c", "c", "b", "b", "b", "b", "a", "a"], + } + ) + df.label = Categorical(df.label, categories=["c", "b", "a"], ordered=True) ax = df.boxplot(by="label") xticklabels = ax.get_xticklabels() - assert [x.get_text() for x in xticklabels] == ['c', 'b', 'a'] + assert [x.get_text() for x in xticklabels] == ["c", "b", "a"] + class TestDataFrameGroupByPlots: def test_boxplot_legacy1(self, hist_df):