Skip to content

Commit 75429df

Browse files
authored
DEPR: enforce deprecation on floats with unit=M or Y (#48671)
1 parent bbb1cdf commit 75429df

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

Diff for: doc/source/whatsnew/v2.0.0.rst

+9
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ Deprecations
138138
-
139139
-
140140

141+
.. ---------------------------------------------------------------------------
142+
143+
.. _whatsnew_200.prior_deprecations:
144+
145+
Removal of prior version deprecations/changes
146+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147+
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
148+
-
149+
141150
.. ---------------------------------------------------------------------------
142151
.. _whatsnew_200.performance:
143152

Diff for: pandas/_libs/tslibs/conversion.pyx

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import warnings
2-
31
import numpy as np
42

5-
from pandas.util._exceptions import find_stack_level
6-
73
cimport numpy as cnp
84
from numpy cimport (
95
int32_t,
@@ -281,11 +277,8 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
281277
# GH#47267 it is clear that 2 "M" corresponds to 1970-02-01,
282278
# but not clear what 2.5 "M" corresponds to, so we will
283279
# disallow that case.
284-
warnings.warn(
285-
"Conversion of non-round float with unit={unit} is ambiguous "
286-
"and will raise in a future version.",
287-
FutureWarning,
288-
stacklevel=find_stack_level(),
280+
raise ValueError(
281+
f"Conversion of non-round float with unit={unit} is ambiguous."
289282
)
290283

291284
ts = cast_from_unit(ts, unit)

Diff for: pandas/tests/scalar/timestamp/test_constructors.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ def test_constructor_int_float_with_YM_unit(self, typ):
4242
def test_constructor_float_not_round_with_YM_unit_deprecated(self):
4343
# GH#47267 avoid the conversions in cast_from-unit
4444

45-
with tm.assert_produces_warning(FutureWarning, match="ambiguous"):
45+
msg = "Conversion of non-round float with unit=[MY] is ambiguous"
46+
with pytest.raises(ValueError, match=msg):
4647
Timestamp(150.5, unit="Y")
4748

48-
with tm.assert_produces_warning(FutureWarning, match="ambiguous"):
49+
with pytest.raises(ValueError, match=msg):
4950
Timestamp(150.5, unit="M")
5051

5152
def test_constructor_datetime64_with_tz(self):

0 commit comments

Comments
 (0)