Skip to content

Commit 2a08299

Browse files
jbrockmendelmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#45668: BUG: unnecessary FutureWarning in sort_values
1 parent 1f64469 commit 2a08299

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
@@ -28,6 +28,7 @@ Fixed regressions
2828
Bug fixes
2929
~~~~~~~~~
3030
- Fixed segfault in :meth:``DataFrame.to_json`` when dumping tz-aware datetimes in Python 3.10 (:issue:`42130`)
31+
- Stopped emitting unnecessary ``FutureWarning`` in :meth:`DataFrame.sort_values` with sparse columns (:issue:`45618`)
3132
- Fixed window aggregations in :meth:`DataFrame.rolling` and :meth:`Series.rolling` to skip over unused elements (:issue:`45647`)
3233
-
3334

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
@@ -278,14 +278,6 @@ def test_fillna_frame(self, data_missing):
278278

279279

280280
class TestMethods(BaseSparseTests, base.BaseMethodsTests):
281-
@pytest.mark.parametrize("ascending", [True, False])
282-
def test_sort_values_frame(self, data_for_sorting, ascending):
283-
msg = "will store that array directly"
284-
with tm.assert_produces_warning(
285-
FutureWarning, match=msg, check_stacklevel=False
286-
):
287-
super().test_sort_values_frame(data_for_sorting, ascending)
288-
289281
def test_combine_le(self, data_repeated):
290282
# We return a Series[SparseArray].__le__ returns a
291283
# 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)