Skip to content

Commit 6449e7a

Browse files
jbrockmendelphofl
authored andcommitted
BUG: unnecessary FutureWarning in sort_values (pandas-dev#45668)
1 parent 8e97256 commit 6449e7a

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

doc/source/whatsnew/v1.4.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Fixed regressions
2929
Bug fixes
3030
~~~~~~~~~
3131
- Fixed segfault in :meth:``DataFrame.to_json`` when dumping tz-aware datetimes in Python 3.10 (:issue:`42130`)
32+
- Stopped emitting unnecessary ``FutureWarning`` in :meth:`DataFrame.sort_values` with sparse columns (:issue:`45618`)
3233
- Fixed window aggregations in :meth:`DataFrame.rolling` and :meth:`Series.rolling` to skip over unused elements (:issue:`45647`)
3334
-
3435

pandas/core/sorting.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Iterable,
1111
Sequence,
1212
)
13+
import warnings
1314

1415
import numpy as np
1516

@@ -320,7 +321,12 @@ def lexsort_indexer(
320321
keys = [ensure_key_mapped(k, key) for k in keys]
321322

322323
for k, order in zip(keys, orders):
323-
cat = Categorical(k, ordered=True)
324+
with warnings.catch_warnings():
325+
# TODO(2.0): unnecessary once deprecation is enforced
326+
# GH#45618 don't issue warning user can't do anything about
327+
warnings.filterwarnings("ignore", ".*SparseArray.*", category=FutureWarning)
328+
329+
cat = Categorical(k, ordered=True)
324330

325331
if na_position not in ["last", "first"]:
326332
raise ValueError(f"invalid na_position: {na_position}")

pandas/tests/extension/test_sparse.py

-8
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,6 @@ def test_fillna_frame(self, data_missing):
293293

294294

295295
class TestMethods(BaseSparseTests, base.BaseMethodsTests):
296-
@pytest.mark.parametrize("ascending", [True, False])
297-
def test_sort_values_frame(self, data_for_sorting, ascending):
298-
msg = "will store that array directly"
299-
with tm.assert_produces_warning(
300-
FutureWarning, match=msg, check_stacklevel=False
301-
):
302-
super().test_sort_values_frame(data_for_sorting, ascending)
303-
304296
def test_combine_le(self, data_repeated):
305297
# We return a Series[SparseArray].__le__ returns a
306298
# Series[Sparse[bool]]

pandas/tests/frame/methods/test_sort_values.py

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515

1616

1717
class TestDataFrameSortValues:
18+
def test_sort_values_sparse_no_warning(self):
19+
# GH#45618
20+
# TODO(2.0): test will be unnecessary
21+
ser = pd.Series(Categorical(["a", "b", "a"], categories=["a", "b", "c"]))
22+
df = pd.get_dummies(ser, sparse=True)
23+
24+
with tm.assert_produces_warning(None):
25+
# No warnings about constructing Index from SparseArray
26+
df.sort_values(by=df.columns.tolist())
27+
1828
def test_sort_values(self):
1929
frame = DataFrame(
2030
[[1, 1, 2], [3, 1, 0], [4, 5, 6]], index=[1, 2, 3], columns=list("ABC")

0 commit comments

Comments
 (0)