diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index b71d294b97f9a..7fe970ef36e14 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -845,6 +845,7 @@ Other Deprecations - Deprecated unused arguments ``encoding`` and ``verbose`` in :meth:`Series.to_excel` and :meth:`DataFrame.to_excel` (:issue:`47912`) - Deprecated producing a single element when iterating over a :class:`DataFrameGroupBy` or a :class:`SeriesGroupBy` that has been grouped by a list of length 1; A tuple of length one will be returned instead (:issue:`42795`) - Fixed up warning message of deprecation of :meth:`MultiIndex.lesort_depth` as public method, as the message previously referred to :meth:`MultiIndex.is_lexsorted` instead (:issue:`38701`) +- Deprecated the ``sort_columns`` argument in :meth:`DataFrame.plot` and :meth:`Series.plot` (:issue:`47563`). .. --------------------------------------------------------------------------- .. _whatsnew_150.performance: diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 0d69a52eb15f1..15865d73c19bf 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1,11 +1,13 @@ from __future__ import annotations import importlib +import inspect import types from typing import ( TYPE_CHECKING, Sequence, ) +import warnings from pandas._config import get_option @@ -14,6 +16,7 @@ Appender, Substitution, ) +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( is_integer, @@ -755,6 +758,11 @@ class PlotAccessor(PandasObject): If True, create stacked plot. sort_columns : bool, default False Sort column names to determine plot ordering. + + .. deprecated:: 1.5.0 + The `sort_columns` arguments is deprecated and will be removed in a + future version. + secondary_y : bool or sequence, default False Whether to plot on the secondary y-axis if a list/tuple, which columns to plot on secondary y-axis. @@ -875,6 +883,14 @@ def _get_call_args(backend_name, data, args, kwargs): "expected Series or DataFrame" ) + if "sort_columns" in args + tuple(kwargs.keys()): + warnings.warn( + "`sort_columns` is deprecated and will be removed in a future " + "version.", + FutureWarning, + stacklevel=find_stack_level(inspect.currentframe()), + ) + if args and isinstance(data, ABCSeries): positional_args = str(args)[1:-1] keyword_args = ", ".join( diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index 538c9c2fb5059..e06d08e7dfdc9 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -2215,6 +2215,16 @@ def test_secondary_y(self, secondary_y): assert ax.get_ylim() == (0, 100) assert ax.get_yticks()[0] == 99 + def test_sort_columns_deprecated(self): + df = DataFrame({"a": [1, 2], "b": [3, 4]}) + + with tm.assert_produces_warning(FutureWarning): + df.plot.box("a", sort_columns=True) + df.plot.box(sort_columns=False) + + with tm.assert_produces_warning(False): + df.plot.box("a") + def _generate_4_axes_via_gridspec(): import matplotlib as mpl