From 8b72297c8799725e98cb2c6aee664325b752194f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yago=20Gonz=C3=A1lez?= Date: Tue, 16 Aug 2022 02:07:00 +0200 Subject: [PATCH] DEPR: `sort_columns` in `plot` (#47563) (#48073) --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/plotting/_core.py | 17 +++++++++++++++++ pandas/tests/plotting/frame/test_frame.py | 13 +++++++++++++ 3 files changed, 31 insertions(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 02db4cbe0e8a5..a1a2149da7cf6 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -847,6 +847,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..96f9abb301471 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1,11 +1,14 @@ from __future__ import annotations import importlib +import inspect +import itertools import types from typing import ( TYPE_CHECKING, Sequence, ) +import warnings from pandas._config import get_option @@ -14,6 +17,7 @@ Appender, Substitution, ) +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( is_integer, @@ -755,6 +759,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 +884,14 @@ def _get_call_args(backend_name, data, args, kwargs): "expected Series or DataFrame" ) + if "sort_columns" in itertools.chain(args, 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..b38c9adb0a893 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -2215,6 +2215,19 @@ 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): + # GH 47563 + df = DataFrame({"a": [1, 2], "b": [3, 4]}) + + with tm.assert_produces_warning(FutureWarning): + df.plot.box("a", sort_columns=True) + + with tm.assert_produces_warning(FutureWarning): + 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