diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 10b56011c9640..499461d9ebcf0 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1354,6 +1354,7 @@ Other - Bug in ``divmod`` and ``rdivmod`` with :class:`DataFrame`, :class:`Series`, and :class:`Index` with ``bool`` dtypes failing to raise, which was inconsistent with ``__floordiv__`` behavior (:issue:`46043`) - Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`) - Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`) +- Bug in :class: `BoxPlot` when column order was not respected in ticklabels. (:issue: `50427`) - Bug when calling :py:func:`copy.copy` on a :class:`DataFrame` or :class:`Series` which would return a deep copy instead of a shallow copy (:issue:`62971`) - Deprecated the keyword ``check_datetimelike_compat`` in :meth:`testing.assert_frame_equal` and :meth:`testing.assert_series_equal` (:issue:`55638`) - Fixed bug in :meth:`Series.replace` and :meth:`DataFrame.replace` when trying to replace :class:`NA` values in a :class:`Float64Dtype` object with ``np.nan``; this now works with ``pd.set_option("mode.nan_is_na", False)`` and is irrelevant otherwise (:issue:`55127`) diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index 4bb185c51478f..ebcba34043509 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 levels[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 2267b6197cd80..d22e3bf885547 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,20 @@ 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_ordered_ticklabel(self, vert): + # GH 50427 + 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):