From e2a0ca660704ef723ddc806f820db8e79f148625 Mon Sep 17 00:00:00 2001 From: Marco Edward Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Tue, 27 Sep 2022 11:05:19 +0100 Subject: [PATCH] Backport PR #48785: BUG: still emitting unnecessary FutureWarning in DataFrame.sort_values with sparse columns --- doc/source/whatsnew/v1.5.1.rst | 1 + pandas/core/sorting.py | 4 +++- pandas/tests/frame/methods/test_sort_values.py | 5 +++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index a7bdf90c0f333..8b73cd84e9e00 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -86,6 +86,7 @@ Bug fixes - Bug in :meth:`Series.__getitem__` not falling back to positional for integer keys and boolean :class:`Index` (:issue:`48653`) - Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`) - Bug in :meth:`DataFrame.pivot_table` raising unexpected ``FutureWarning`` when setting datetime column as index (:issue:`48683`) +- Bug in :meth:`DataFrame.sort_values` emitting unnecessary ``FutureWarning`` when called on :class:`DataFrame` with boolean sparse columns (:issue:`48784`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/sorting.py b/pandas/core/sorting.py index 16facfc915e40..1d1a93b8d1ce2 100644 --- a/pandas/core/sorting.py +++ b/pandas/core/sorting.py @@ -343,7 +343,9 @@ def lexsort_indexer( 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) + warnings.filterwarnings( + "ignore", ".*(SparseArray|SparseDtype).*", category=FutureWarning + ) cat = Categorical(k, ordered=True) diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index 9f3fcb1db546d..51b590263f893 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -15,11 +15,12 @@ class TestDataFrameSortValues: - def test_sort_values_sparse_no_warning(self): + @pytest.mark.parametrize("dtype", [np.uint8, bool]) + def test_sort_values_sparse_no_warning(self, dtype): # 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) + df = pd.get_dummies(ser, dtype=dtype, sparse=True) with tm.assert_produces_warning(None): # No warnings about constructing Index from SparseArray