Skip to content

DEPR: enforce deprecation on floats with unit=M or Y #48671

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 7 commits into from
Oct 19, 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
9 changes: 9 additions & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ Deprecations
-
-

.. ---------------------------------------------------------------------------

.. _whatsnew_200.prior_deprecations:

Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
-

.. ---------------------------------------------------------------------------
.. _whatsnew_200.performance:

Expand Down
11 changes: 2 additions & 9 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import warnings

import numpy as np

from pandas.util._exceptions import find_stack_level

cimport numpy as cnp
from numpy cimport (
int32_t,
Expand Down Expand Up @@ -281,11 +277,8 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
# 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=find_stack_level(),
raise ValueError(
f"Conversion of non-round float with unit={unit} is ambiguous."
)

ts = cast_from_unit(ts, unit)
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ def test_constructor_int_float_with_YM_unit(self, typ):
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"):
msg = "Conversion of non-round float with unit=[MY] is ambiguous"
with pytest.raises(ValueError, match=msg):
Timestamp(150.5, unit="Y")

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

def test_constructor_datetime64_with_tz(self):
Expand Down