From 3dcfce58e713fc6f36d4b9270c66ec1d28fb1588 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 27 Jan 2022 16:29:39 -0800 Subject: [PATCH 1/3] BUG: unnecessary FutureWarning in sort_values --- doc/source/whatsnew/v1.4.1.rst | 1 + pandas/core/sorting.py | 8 +++++++- pandas/tests/frame/methods/test_sort_values.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.1.rst b/doc/source/whatsnew/v1.4.1.rst index 1bd8da2d2b03c..7374a6e355511 100644 --- a/doc/source/whatsnew/v1.4.1.rst +++ b/doc/source/whatsnew/v1.4.1.rst @@ -25,6 +25,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Fixed segfault in :meth:``DataFrame.to_json`` when dumping tz-aware datetimes in Python 3.10 (:issue:`42130`) +- Stopped emitting unnecessary ``FutureWarning`` in :meth:`DataFrame.sort_values` with sparse columns (:issue:`45618``) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/sorting.py b/pandas/core/sorting.py index ac306b1687381..7ab53ccf7cb8d 100644 --- a/pandas/core/sorting.py +++ b/pandas/core/sorting.py @@ -10,6 +10,7 @@ Iterable, Sequence, ) +import warnings import numpy as np @@ -320,7 +321,12 @@ def lexsort_indexer( keys = [ensure_key_mapped(k, key) for k in keys] for k, order in zip(keys, orders): - cat = Categorical(k, ordered=True) + with warnings.catch_warnings(): + # TODO(2.0): unnecessary once deprecation is enforced + # GH#45618 don't issue warning user can't do anything about + warnings.filterwarnings("ignore", ".*SparseArray.*", category=FutureWarning) + + cat = Categorical(k, ordered=True) if na_position not in ["last", "first"]: raise ValueError(f"invalid na_position: {na_position}") diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index 0a05712489147..9f3fcb1db546d 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -15,6 +15,16 @@ class TestDataFrameSortValues: + def test_sort_values_sparse_no_warning(self): + # GH#45618 + # TODO(2.0): test will be unnecessary + ser = pd.Series(Categorical(["a", "b", "a"], categories=["a", "b", "c"])) + df = pd.get_dummies(ser, sparse=True) + + with tm.assert_produces_warning(None): + # No warnings about constructing Index from SparseArray + df.sort_values(by=df.columns.tolist()) + def test_sort_values(self): frame = DataFrame( [[1, 1, 2], [3, 1, 0], [4, 5, 6]], index=[1, 2, 3], columns=list("ABC") From 261e03c25b292033205972d624255537a7c1d391 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 28 Jan 2022 22:00:22 -0800 Subject: [PATCH 2/3] de-override sparse tests --- pandas/tests/extension/test_sparse.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index 4f453986ad2c3..3aaf78ba91399 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -293,14 +293,6 @@ def test_fillna_frame(self, data_missing): class TestMethods(BaseSparseTests, base.BaseMethodsTests): - @pytest.mark.parametrize("ascending", [True, False]) - def test_sort_values_frame(self, data_for_sorting, ascending): - msg = "will store that array directly" - with tm.assert_produces_warning( - FutureWarning, match=msg, check_stacklevel=False - ): - super().test_sort_values_frame(data_for_sorting, ascending) - def test_combine_le(self, data_repeated): # We return a Series[SparseArray].__le__ returns a # Series[Sparse[bool]] From 2562cec21fd7f64369198e5386422a1c16f21c12 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 30 Jan 2022 10:28:15 -0800 Subject: [PATCH 3/3] typo fixup --- doc/source/whatsnew/v1.4.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.1.rst b/doc/source/whatsnew/v1.4.1.rst index 084efade2e119..9e4b3479341b3 100644 --- a/doc/source/whatsnew/v1.4.1.rst +++ b/doc/source/whatsnew/v1.4.1.rst @@ -28,7 +28,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Fixed segfault in :meth:``DataFrame.to_json`` when dumping tz-aware datetimes in Python 3.10 (:issue:`42130`) -- Stopped emitting unnecessary ``FutureWarning`` in :meth:`DataFrame.sort_values` with sparse columns (:issue:`45618``) +- Stopped emitting unnecessary ``FutureWarning`` in :meth:`DataFrame.sort_values` with sparse columns (:issue:`45618`) - Fixed window aggregations in :meth:`DataFrame.rolling` and :meth:`Series.rolling` to skip over unused elements (:issue:`45647`) -