Skip to content

Commit b01cc53

Browse files
authored
DEPR: enforce Series constructor with int dtype deprs (#49777)
1 parent d4e3963 commit b01cc53

File tree

4 files changed

+20
-38
lines changed

4 files changed

+20
-38
lines changed

doc/source/whatsnew/v2.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ Removal of prior version deprecations/changes
548548
- Changed behavior of setitem-like operations (``__setitem__``, ``fillna``, ``where``, ``mask``, ``replace``, ``insert``, fill_value for ``shift``) on an object with :class:`DatetimeTZDtype` when using a value with a non-matching timezone, the value will be cast to the object's timezone instead of casting both to object-dtype (:issue:`44243`)
549549
- Changed behavior of :class:`Index`, :class:`Series`, :class:`DataFrame` constructors with floating-dtype data and a :class:`DatetimeTZDtype`, the data are now interpreted as UTC-times instead of wall-times, consistent with how integer-dtype data are treated (:issue:`45573`)
550550
- Changed behavior of :class:`Series` and :class:`DataFrame` constructors with integer dtype and floating-point data containing ``NaN``, this now raises ``IntCastingNaNError`` (:issue:`40110`)
551+
- Changed behavior of :class:`Series` and :class:`DataFrame` constructors with an integer ``dtype`` and values that are too large to losslessly cast to this dtype, this now raises ``ValueError`` (:issue:`41734`)
552+
- Changed behavior of :class:`Series` and :class:`DataFrame` constructors with an integer ``dtype`` and values having either ``datetime64`` or ``timedelta64`` dtypes, this now raises ``TypeError``, use ``values.view("int64")`` instead (:issue:`41770`)
551553
- Removed the deprecated ``base`` and ``loffset`` arguments from :meth:`pandas.DataFrame.resample`, :meth:`pandas.Series.resample` and :class:`pandas.Grouper`. Use ``offset`` or ``origin`` instead (:issue:`31809`)
552554
- Changed behavior of :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``timedelta64[ns]`` dtype and an incompatible ``fill_value``; this now casts to ``object`` dtype instead of raising, consistent with the behavior with other dtypes (:issue:`45746`)
553555
- Change the default argument of ``regex`` for :meth:`Series.str.replace` from ``True`` to ``False``. Additionally, a single character ``pat`` with ``regex=True`` is now treated as a regular expression instead of a string literal. (:issue:`36695`, :issue:`24804`)

pandas/core/dtypes/cast.py

+6-15
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
IntCastingNaNError,
4242
LossySetitemError,
4343
)
44-
from pandas.util._exceptions import find_stack_level
4544
from pandas.util._validators import validate_bool_kwarg
4645

4746
from pandas.core.dtypes.common import (
@@ -1680,25 +1679,17 @@ def maybe_cast_to_integer_array(
16801679

16811680
if casted.dtype < arr.dtype:
16821681
# GH#41734 e.g. [1, 200, 923442] and dtype="int8" -> overflows
1683-
warnings.warn(
1684-
f"Values are too large to be losslessly cast to {dtype}. "
1685-
"In a future version this will raise OverflowError. To retain the "
1686-
f"old behavior, use pd.Series(values).astype({dtype})",
1687-
FutureWarning,
1688-
stacklevel=find_stack_level(),
1682+
raise ValueError(
1683+
f"Values are too large to be losslessly converted to {dtype}. "
1684+
f"To cast anyway, use pd.Series(values).astype({dtype})"
16891685
)
1690-
return casted
16911686

16921687
if arr.dtype.kind in ["m", "M"]:
16931688
# test_constructor_maskedarray_nonfloat
1694-
warnings.warn(
1695-
f"Constructing Series or DataFrame from {arr.dtype} values and "
1696-
f"dtype={dtype} is deprecated and will raise in a future version. "
1697-
"Use values.view(dtype) instead.",
1698-
FutureWarning,
1699-
stacklevel=find_stack_level(),
1689+
raise TypeError(
1690+
f"Constructing a Series or DataFrame from {arr.dtype} values and "
1691+
f"dtype={dtype} is not supported. Use values.view({dtype}) instead."
17001692
)
1701-
return casted
17021693

17031694
# No known cases that get here, but raising explicitly to cover our bases.
17041695
raise ValueError(f"values cannot be losslessly cast to {dtype}")

pandas/tests/frame/test_constructors.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1054,18 +1054,15 @@ def test_constructor_maskedarray_nonfloat(self):
10541054
assert isna(frame).values.all()
10551055

10561056
# cast type
1057-
msg = r"datetime64\[ns\] values and dtype=int64"
1058-
with tm.assert_produces_warning(FutureWarning, match=msg):
1057+
msg = r"datetime64\[ns\] values and dtype=int64 is not supported"
1058+
with pytest.raises(TypeError, match=msg):
10591059
with warnings.catch_warnings():
10601060
warnings.filterwarnings(
10611061
"ignore",
10621062
category=DeprecationWarning,
10631063
message="elementwise comparison failed",
10641064
)
1065-
frame = DataFrame(
1066-
mat, columns=["A", "B", "C"], index=[1, 2], dtype=np.int64
1067-
)
1068-
assert frame.values.dtype == np.int64
1065+
DataFrame(mat, columns=["A", "B", "C"], index=[1, 2], dtype=np.int64)
10691066

10701067
# Check non-masked values
10711068
mat2 = ma.copy(mat)

pandas/tests/series/test_constructors.py

+9-17
Original file line numberDiff line numberDiff line change
@@ -749,25 +749,17 @@ def test_constructor_cast(self):
749749
with pytest.raises(ValueError, match=msg):
750750
Series(["a", "b", "c"], dtype=float)
751751

752-
def test_constructor_signed_int_overflow_deprecation(self):
753-
# GH#41734 disallow silent overflow
754-
msg = "Values are too large to be losslessly cast"
752+
def test_constructor_signed_int_overflow_raises(self):
753+
# GH#41734 disallow silent overflow, enforced in 2.0
754+
msg = "Values are too large to be losslessly converted"
755755
numpy_warning = DeprecationWarning if is_numpy_dev else None
756-
with tm.assert_produces_warning(
757-
(FutureWarning, numpy_warning), match=msg, check_stacklevel=False
758-
):
759-
ser = Series([1, 200, 923442], dtype="int8")
760-
761-
expected = Series([1, -56, 50], dtype="int8")
762-
tm.assert_series_equal(ser, expected)
763-
764-
with tm.assert_produces_warning(
765-
(FutureWarning, numpy_warning), match=msg, check_stacklevel=False
766-
):
767-
ser = Series([1, 200, 923442], dtype="uint8")
756+
with pytest.raises(ValueError, match=msg):
757+
with tm.assert_produces_warning(numpy_warning):
758+
Series([1, 200, 923442], dtype="int8")
768759

769-
expected = Series([1, 200, 50], dtype="uint8")
770-
tm.assert_series_equal(ser, expected)
760+
with pytest.raises(ValueError, match=msg):
761+
with tm.assert_produces_warning(numpy_warning):
762+
Series([1, 200, 923442], dtype="uint8")
771763

772764
@pytest.mark.parametrize(
773765
"values",

0 commit comments

Comments
 (0)