From d9ca1c0a1d31b67f7072fc717bfecace740bd1ad Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 12 Jan 2025 10:05:12 -0500 Subject: [PATCH 1/2] ENH: Enable .mode to sort with NA values --- pandas/core/algorithms.py | 3 ++- pandas/tests/frame/test_reductions.py | 17 ++--------------- pandas/tests/reductions/test_reductions.py | 13 +++---------- 3 files changed, 7 insertions(+), 26 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 56f8adda93251..c90dfc59346c6 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1012,7 +1012,8 @@ def mode( return npresult, res_mask # type: ignore[return-value] try: - npresult = np.sort(npresult) + # npresult = np.sort(npresult) + npresult = safe_sort(npresult) except TypeError as err: warnings.warn( f"Unable to sort modes: {err}", diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index fde4dfeed9c55..04b1456cdbea6 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -672,23 +672,10 @@ def test_mode_dropna(self, dropna, expected): expected = DataFrame(expected) tm.assert_frame_equal(result, expected) - def test_mode_sortwarning(self, using_infer_string): - # Check for the warning that is raised when the mode - # results cannot be sorted - + def test_mode_sort_with_na(self, using_infer_string): df = DataFrame({"A": [np.nan, np.nan, "a", "a"]}) expected = DataFrame({"A": ["a", np.nan]}) - - # TODO(infer_string) avoid this UserWarning for python storage - warning = ( - None - if using_infer_string and df.A.dtype.storage == "pyarrow" - else UserWarning - ) - with tm.assert_produces_warning(warning, match="Unable to sort modes"): - result = df.mode(dropna=False) - result = result.sort_values(by="A").reset_index(drop=True) - + result = df.mode(dropna=False) tm.assert_frame_equal(result, expected) def test_mode_empty_df(self): diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 476978aeab15a..a7bb80727206e 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -1607,17 +1607,10 @@ def test_mode_intoverflow(self, dropna, expected1, expected2): expected2 = Series(expected2, dtype=np.uint64) tm.assert_series_equal(result, expected2) - def test_mode_sortwarning(self): - # Check for the warning that is raised when the mode - # results cannot be sorted - - expected = Series(["foo", np.nan], dtype=object) + def test_mode_sort_with_na(self): s = Series([1, "foo", "foo", np.nan, np.nan]) - - with tm.assert_produces_warning(UserWarning, match="Unable to sort modes"): - result = s.mode(dropna=False) - result = result.sort_values().reset_index(drop=True) - + expected = Series(["foo", np.nan], dtype=object) + result = s.mode(dropna=False) tm.assert_series_equal(result, expected) def test_mode_boolean_with_na(self): From a41b0653916bec97e1868855f7c8ae4516573df0 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 12 Jan 2025 10:06:58 -0500 Subject: [PATCH 2/2] Cleanup --- pandas/core/algorithms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index c90dfc59346c6..eefe08859c1e9 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1012,7 +1012,6 @@ def mode( return npresult, res_mask # type: ignore[return-value] try: - # npresult = np.sort(npresult) npresult = safe_sort(npresult) except TypeError as err: warnings.warn(