Skip to content

Commit 019574d

Browse files
authored
DEPR: Timestamp(150.5, unit=Y) (#47267)
* DEPR: Timestamp(150.5, unit=Y) * GH refs
1 parent cd3fdd5 commit 019574d

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

doc/source/whatsnew/v1.5.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,8 @@ Other Deprecations
684684
- Deprecated the ``closed`` argument in :class:`IntervalArray` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
685685
- Deprecated the ``closed`` argument in :class:`intervaltree` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
686686
- Deprecated the ``closed`` argument in :class:`ArrowInterval` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
687+
- Deprecated allowing ``unit="M"`` or ``unit="Y"`` in :class:`Timestamp` constructor with a non-round float value (:issue:`47267`)
688+
-
687689

688690
.. ---------------------------------------------------------------------------
689691
.. _whatsnew_150.performance:

pandas/_libs/tslibs/conversion.pyx

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
cimport cython
22

3+
import warnings
4+
35
import numpy as np
46

57
cimport numpy as cnp
@@ -255,6 +257,18 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
255257
if ts != ts or ts == NPY_NAT:
256258
obj.value = NPY_NAT
257259
else:
260+
if unit in ["Y", "M"]:
261+
if ts != int(ts):
262+
# GH#47267 it is clear that 2 "M" corresponds to 1970-02-01,
263+
# but not clear what 2.5 "M" corresponds to, so we will
264+
# disallow that case.
265+
warnings.warn(
266+
"Conversion of non-round float with unit={unit} is ambiguous "
267+
"and will raise in a future version.",
268+
FutureWarning,
269+
stacklevel=1,
270+
)
271+
258272
ts = cast_from_unit(ts, unit)
259273
obj.value = ts
260274
dt64_to_dtstruct(ts, &obj.dts)

pandas/tests/scalar/timestamp/test_constructors.py

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525

2626

2727
class TestTimestampConstructors:
28+
def test_constructor_float_not_round_with_YM_unit_deprecated(self):
29+
# GH#47267 avoid the conversions in cast_from-unit
30+
31+
with tm.assert_produces_warning(FutureWarning, match="ambiguous"):
32+
Timestamp(150.5, unit="Y")
33+
34+
with tm.assert_produces_warning(FutureWarning, match="ambiguous"):
35+
Timestamp(150.5, unit="M")
36+
2837
def test_constructor_datetime64_with_tz(self):
2938
# GH#42288, GH#24559
3039
dt = np.datetime64("1970-01-01 05:00:00")

0 commit comments

Comments
 (0)