Skip to content

Commit f1b255d

Browse files
authored
DEPR: numeric_only=None in DataFrame.rank (#45059)
1 parent 3bbaa89 commit f1b255d

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

doc/source/whatsnew/v0.18.0.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,9 @@ New signature
669669

670670
.. ipython:: python
671671
672-
pd.Series([0,1]).rank(axis=0, method='average', numeric_only=None,
672+
pd.Series([0,1]).rank(axis=0, method='average', numeric_only=False,
673673
na_option='keep', ascending=True, pct=False)
674-
pd.DataFrame([0,1]).rank(axis=0, method='average', numeric_only=None,
674+
pd.DataFrame([0,1]).rank(axis=0, method='average', numeric_only=False,
675675
na_option='keep', ascending=True, pct=False)
676676
677677

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ Other Deprecations
547547
- Deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`)
548548
- Deprecated :meth:`Index.__getitem__` with a bool key; use ``index.values[key]`` to get the old behavior (:issue:`44051`)
549549
- Deprecated downcasting column-by-column in :meth:`DataFrame.where` with integer-dtypes (:issue:`44597`)
550+
- Deprecated ``numeric_only=None`` in :meth:`DataFrame.rank`; in a future version ``numeric_only`` must be either ``True`` or ``False`` (the default) (:issue:`45036`)
550551
- Deprecated :meth:`NaT.freq` (:issue:`45071`)
551552
-
552553

pandas/core/generic.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -8406,7 +8406,7 @@ def rank(
84068406
self: NDFrameT,
84078407
axis=0,
84088408
method: str = "average",
8409-
numeric_only: bool_t | None = None,
8409+
numeric_only: bool_t | None | lib.NoDefault = lib.no_default,
84108410
na_option: str = "keep",
84118411
ascending: bool_t = True,
84128412
pct: bool_t = False,
@@ -8492,6 +8492,20 @@ def rank(
84928492
3 spider 8.0 4.0 4.0 4.0 1.000
84938493
4 snake NaN NaN NaN 5.0 NaN
84948494
"""
8495+
warned = False
8496+
if numeric_only is None:
8497+
# GH#45036
8498+
warnings.warn(
8499+
f"'numeric_only=None' in {type(self).__name__}.rank is deprecated "
8500+
"and will raise in a future version. Pass either 'True' or "
8501+
"'False'. 'False' will be the default.",
8502+
FutureWarning,
8503+
stacklevel=find_stack_level(),
8504+
)
8505+
warned = True
8506+
elif numeric_only is lib.no_default:
8507+
numeric_only = None
8508+
84958509
axis = self._get_axis_number(axis)
84968510

84978511
if na_option not in {"keep", "top", "bottom"}:
@@ -8521,6 +8535,16 @@ def ranker(data):
85218535
return ranker(self)
85228536
except TypeError:
85238537
numeric_only = True
8538+
if not warned:
8539+
# Only warn here if we didn't already issue a warning above
8540+
# GH#45036
8541+
warnings.warn(
8542+
f"Dropping of nuisance columns in {type(self).__name__}.rank "
8543+
"is deprecated; in a future version this will raise TypeError. "
8544+
"Select only valid columns before calling rank.",
8545+
FutureWarning,
8546+
stacklevel=find_stack_level(),
8547+
)
85248548

85258549
if numeric_only:
85268550
data = self._get_numeric_data()

pandas/tests/frame/methods/test_rank.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ def test_rank_mixed_frame(self, float_string_frame):
136136
float_string_frame["datetime"] = datetime.now()
137137
float_string_frame["timedelta"] = timedelta(days=1, seconds=1)
138138

139-
result = float_string_frame.rank(1)
139+
with tm.assert_produces_warning(FutureWarning, match="numeric_only=None"):
140+
float_string_frame.rank(numeric_only=None)
141+
with tm.assert_produces_warning(FutureWarning, match="Dropping of nuisance"):
142+
result = float_string_frame.rank(1)
140143
expected = float_string_frame.rank(1, numeric_only=True)
141144
tm.assert_frame_equal(result, expected)
142145

@@ -489,5 +492,7 @@ def test_rank_object_first(self, frame_or_series, na_option, ascending, expected
489492
)
490493
def test_rank_mixed_axis_zero(self, data, expected):
491494
df = DataFrame(data)
492-
result = df.rank()
495+
msg = "Dropping of nuisance columns"
496+
with tm.assert_produces_warning(FutureWarning, match=msg):
497+
result = df.rank()
493498
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)