Skip to content

BUG: unnecessary FutureWarning in sort_values #45668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +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``)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Extra backtick at the end of the issue number

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, will update

- Fixed window aggregations in :meth:`DataFrame.rolling` and :meth:`Series.rolling` to skip over unused elements (:issue:`45647`)
-

Expand Down
8 changes: 7 additions & 1 deletion pandas/core/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Iterable,
Sequence,
)
import warnings

import numpy as np

Expand Down Expand Up @@ -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}")
Expand Down
8 changes: 0 additions & 8 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/methods/test_sort_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down