Skip to content

Commit 5c745bd

Browse files
authored
DEPR: float/int(Series[single_elemet]) (#51131)
1 parent 605d446 commit 5c745bd

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

doc/source/whatsnew/v2.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ Deprecations
780780
- :meth:`Index.is_object` has been deprecated. Use :func:`pandas.api.types.is_object_dtype` instead (:issue:`50042`)
781781
- :meth:`Index.is_interval` has been deprecated. Use :func:`pandas.api.types.is_intterval_dtype` instead (:issue:`50042`)
782782
- 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`)
783-
-
783+
- 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`)
784784

785785
.. ---------------------------------------------------------------------------
786786
.. _whatsnew_200.prior_deprecations:

pandas/core/series.py

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
cast,
2020
overload,
2121
)
22+
import warnings
2223
import weakref
2324

2425
import numpy as np
@@ -81,6 +82,7 @@
8182
Substitution,
8283
doc,
8384
)
85+
from pandas.util._exceptions import find_stack_level
8486
from pandas.util._validators import (
8587
validate_ascending,
8688
validate_bool_kwarg,
@@ -216,6 +218,13 @@ def _coerce_method(converter):
216218

217219
def wrapper(self):
218220
if len(self) == 1:
221+
warnings.warn(
222+
f"Calling {converter.__name__} on a single element Series is "
223+
"deprecated and will raise a TypeError in the future. "
224+
f"Use {converter.__name__}(ser.iloc[0]) instead",
225+
FutureWarning,
226+
stacklevel=find_stack_level(),
227+
)
219228
return converter(self.iloc[0])
220229
raise TypeError(f"cannot convert the series to {converter}")
221230

pandas/tests/apply/test_invalid_arg.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,15 @@ def test_agg_none_to_type():
277277
df = DataFrame({"a": [None]})
278278
msg = re.escape("int() argument must be a string")
279279
with pytest.raises(TypeError, match=msg):
280-
df.agg({"a": int})
280+
df.agg({"a": lambda x: int(x.iloc[0])})
281281

282282

283283
def test_transform_none_to_type():
284284
# GH#34377
285285
df = DataFrame({"a": [None]})
286286
msg = "argument must be a"
287287
with pytest.raises(TypeError, match=msg):
288-
df.transform({"a": int})
288+
df.transform({"a": lambda x: int(x.iloc[0])})
289289

290290

291291
@pytest.mark.parametrize(

pandas/tests/series/test_api.py

+7
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,10 @@ def test_numeric_only(self, kernel, has_numeric_only, dtype):
287287
else:
288288
# reducer
289289
assert result == expected
290+
291+
292+
@pytest.mark.parametrize("converter", [int, float, complex])
293+
def test_float_int_deprecated(converter):
294+
# GH 51101
295+
with tm.assert_produces_warning(FutureWarning):
296+
assert converter(Series([1])) == converter(1)

pandas/tests/series/test_constructors.py

-4
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ def test_scalar_conversion(self):
145145
scalar = Series(0.5)
146146
assert not isinstance(scalar, float)
147147

148-
# Coercion
149-
assert float(Series([1.0])) == 1.0
150-
assert int(Series([1.0])) == 1
151-
152148
def test_scalar_extension_dtype(self, ea_scalar_and_dtype):
153149
# GH 28401
154150

0 commit comments

Comments
 (0)