Skip to content

DEPR: Timestamp(150.5, unit=Y) #47267

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 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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: 2 additions & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ Other Deprecations
- Deprecated the ``closed`` argument in :class:`IntervalArray` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
- Deprecated the ``closed`` argument in :class:`intervaltree` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
- Deprecated the ``closed`` argument in :class:`ArrowInterval` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
- Deprecated allowing ``unit="M"`` or ``unit="Y"`` in :class:`Timestamp` constructor with a non-round float value (:issue:`47267`)
-

.. ---------------------------------------------------------------------------
.. _whatsnew_150.performance:
Expand Down
14 changes: 14 additions & 0 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cimport cython

import warnings

import numpy as np

cimport numpy as cnp
Expand Down Expand Up @@ -255,6 +257,18 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
if ts != ts or ts == NPY_NAT:
obj.value = NPY_NAT
else:
if unit in ["Y", "M"]:
if ts != int(ts):
# GH#47267 it is clear that 2 "M" corresponds to 1970-02-01,
# but not clear what 2.5 "M" corresponds to, so we will
# disallow that case.
warnings.warn(
"Conversion of non-round float with unit={unit} is ambiguous "
"and will raise in a future version.",
FutureWarning,
stacklevel=1,
)

ts = cast_from_unit(ts, unit)
obj.value = ts
dt64_to_dtstruct(ts, &obj.dts)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@


class TestTimestampConstructors:
def test_constructor_float_not_round_with_YM_unit_deprecated(self):
# GH#47267 avoid the conversions in cast_from-unit

with tm.assert_produces_warning(FutureWarning, match="ambiguous"):
Timestamp(150.5, unit="Y")

with tm.assert_produces_warning(FutureWarning, match="ambiguous"):
Timestamp(150.5, unit="M")

def test_constructor_datetime64_with_tz(self):
# GH#42288, GH#24559
dt = np.datetime64("1970-01-01 05:00:00")
Expand Down