Skip to content

DEPR: float/int(Series[single_elemet]) #51131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ Deprecations
- :meth:`Index.is_object` has been deprecated. Use :func:`pandas.api.types.is_object_dtype` instead (:issue:`50042`)
- :meth:`Index.is_interval` has been deprecated. Use :func:`pandas.api.types.is_intterval_dtype` instead (:issue:`50042`)
- Deprecated ``all`` and ``any`` reductions with ``datetime64`` and :class:`DatetimeTZDtype` dtypes, use e.g. ``(obj != pd.Timestamp(0), tz=obj.tz).all()`` instead (:issue:`34479`)
-
- Deprecated calling ``float`` or ``int`` on a single element :class:`Series` to return a ``float`` or ``int`` respectively. Extract the element before calling ``float`` or ``int`` instead (:issue:`51101`)

.. ---------------------------------------------------------------------------
.. _whatsnew_200.prior_deprecations:
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
cast,
overload,
)
import warnings
import weakref

import numpy as np
Expand Down Expand Up @@ -81,6 +82,7 @@
Substitution,
doc,
)
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import (
validate_ascending,
validate_bool_kwarg,
Expand Down Expand Up @@ -216,6 +218,13 @@ def _coerce_method(converter):

def wrapper(self):
if len(self) == 1:
warnings.warn(
f"Calling {converter.__name__} on a single element Series is "
"deprecated and will raise a TypeError in the future. "
f"Use {converter.__name__}(ser.iloc[0]) instead",
FutureWarning,
stacklevel=find_stack_level(),
)
return converter(self.iloc[0])
raise TypeError(f"cannot convert the series to {converter}")

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,10 @@ def test_numeric_only(self, kernel, has_numeric_only, dtype):
else:
# reducer
assert result == expected


@pytest.mark.parametrize("converter", [int, float])
def test_float_int_deprecated(converter):
# GH 51101
with tm.assert_produces_warning(FutureWarning):
assert converter(Series([1])) == converter(1)