Skip to content

Commit

Permalink
DEPR: numeric_only=None in DataFrame.rank (#45059)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Dec 27, 2021
1 parent 3bbaa89 commit f1b255d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v0.18.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,9 @@ New signature

.. ipython:: python
pd.Series([0,1]).rank(axis=0, method='average', numeric_only=None,
pd.Series([0,1]).rank(axis=0, method='average', numeric_only=False,
na_option='keep', ascending=True, pct=False)
pd.DataFrame([0,1]).rank(axis=0, method='average', numeric_only=None,
pd.DataFrame([0,1]).rank(axis=0, method='average', numeric_only=False,
na_option='keep', ascending=True, pct=False)
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ Other Deprecations
- Deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`)
- Deprecated :meth:`Index.__getitem__` with a bool key; use ``index.values[key]`` to get the old behavior (:issue:`44051`)
- Deprecated downcasting column-by-column in :meth:`DataFrame.where` with integer-dtypes (:issue:`44597`)
- Deprecated ``numeric_only=None`` in :meth:`DataFrame.rank`; in a future version ``numeric_only`` must be either ``True`` or ``False`` (the default) (:issue:`45036`)
- Deprecated :meth:`NaT.freq` (:issue:`45071`)
-

Expand Down
26 changes: 25 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8406,7 +8406,7 @@ def rank(
self: NDFrameT,
axis=0,
method: str = "average",
numeric_only: bool_t | None = None,
numeric_only: bool_t | None | lib.NoDefault = lib.no_default,
na_option: str = "keep",
ascending: bool_t = True,
pct: bool_t = False,
Expand Down Expand Up @@ -8492,6 +8492,20 @@ def rank(
3 spider 8.0 4.0 4.0 4.0 1.000
4 snake NaN NaN NaN 5.0 NaN
"""
warned = False
if numeric_only is None:
# GH#45036
warnings.warn(
f"'numeric_only=None' in {type(self).__name__}.rank is deprecated "
"and will raise in a future version. Pass either 'True' or "
"'False'. 'False' will be the default.",
FutureWarning,
stacklevel=find_stack_level(),
)
warned = True
elif numeric_only is lib.no_default:
numeric_only = None

axis = self._get_axis_number(axis)

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

if numeric_only:
data = self._get_numeric_data()
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/frame/methods/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ def test_rank_mixed_frame(self, float_string_frame):
float_string_frame["datetime"] = datetime.now()
float_string_frame["timedelta"] = timedelta(days=1, seconds=1)

result = float_string_frame.rank(1)
with tm.assert_produces_warning(FutureWarning, match="numeric_only=None"):
float_string_frame.rank(numeric_only=None)
with tm.assert_produces_warning(FutureWarning, match="Dropping of nuisance"):
result = float_string_frame.rank(1)
expected = float_string_frame.rank(1, numeric_only=True)
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -489,5 +492,7 @@ def test_rank_object_first(self, frame_or_series, na_option, ascending, expected
)
def test_rank_mixed_axis_zero(self, data, expected):
df = DataFrame(data)
result = df.rank()
msg = "Dropping of nuisance columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.rank()
tm.assert_frame_equal(result, expected)

0 comments on commit f1b255d

Please sign in to comment.