Skip to content

BUG: when flooring, ambiguous parameter unnecessarily used (and raising Error) #50024

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 15 commits into from
Dec 12, 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
1 change: 0 additions & 1 deletion doc/source/whatsnew/v1.5.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Bug fixes
Other
~~~~~
-
-

.. ---------------------------------------------------------------------------
.. _whatsnew_153.contributors:
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ Timezones
^^^^^^^^^
- Bug in :meth:`Series.astype` and :meth:`DataFrame.astype` with object-dtype containing multiple timezone-aware ``datetime`` objects with heterogeneous timezones to a :class:`DatetimeTZDtype` incorrectly raising (:issue:`32581`)
- Bug in :func:`to_datetime` was failing to parse date strings with timezone name when ``format`` was specified with ``%Z`` (:issue:`49748`)
-
- Better error message when passing invalid values to ``ambiguous`` parameter in :meth:`Timestamp.tz_localize` (:issue:`49565`)

Numeric
^^^^^^^
Expand Down
9 changes: 6 additions & 3 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ construction requirements, we need to do object instantiation in python
(see Timestamp class below). This will serve as a C extension type that
shadows the python class, where we do any heavy lifting.
"""
import warnings

import warnings
cimport cython

import numpy as np
Expand Down Expand Up @@ -1946,8 +1946,11 @@ default 'raise'
>>> pd.NaT.tz_localize()
NaT
"""
if ambiguous == "infer":
raise ValueError("Cannot infer offset with only one time.")
if not isinstance(ambiguous, bool) and ambiguous not in {"NaT", "raise"}:
raise ValueError(
"'ambiguous' parameter must be one of: "
"True, False, 'NaT', 'raise' (default)"
)

nonexistent_options = ("raise", "NaT", "shift_forward", "shift_backward")
if nonexistent not in nonexistent_options and not PyDelta_Check(nonexistent):
Expand Down
15 changes: 9 additions & 6 deletions pandas/tests/scalar/timestamp/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
datetime,
timedelta,
)
import re

import dateutil
from dateutil.tz import (
Expand Down Expand Up @@ -102,7 +103,10 @@ def test_tz_localize_ambiguous(self):
ts_no_dst = ts.tz_localize("US/Eastern", ambiguous=False)

assert (ts_no_dst.value - ts_dst.value) / 1e9 == 3600
msg = "Cannot infer offset with only one time"
msg = re.escape(
"'ambiguous' parameter must be one of: "
"True, False, 'NaT', 'raise' (default)"
)
with pytest.raises(ValueError, match=msg):
ts.tz_localize("US/Eastern", ambiguous="infer")

Expand Down Expand Up @@ -182,8 +186,8 @@ def test_tz_localize_ambiguous_compat(self):

pytz_zone = "Europe/London"
dateutil_zone = "dateutil/Europe/London"
result_pytz = naive.tz_localize(pytz_zone, ambiguous=0)
result_dateutil = naive.tz_localize(dateutil_zone, ambiguous=0)
result_pytz = naive.tz_localize(pytz_zone, ambiguous=False)
result_dateutil = naive.tz_localize(dateutil_zone, ambiguous=False)
assert result_pytz.value == result_dateutil.value
assert result_pytz.value == 1382835600000000000

Expand All @@ -194,8 +198,8 @@ def test_tz_localize_ambiguous_compat(self):
assert str(result_pytz) == str(result_dateutil)

# 1 hour difference
result_pytz = naive.tz_localize(pytz_zone, ambiguous=1)
result_dateutil = naive.tz_localize(dateutil_zone, ambiguous=1)
result_pytz = naive.tz_localize(pytz_zone, ambiguous=True)
result_dateutil = naive.tz_localize(dateutil_zone, ambiguous=True)
assert result_pytz.value == result_dateutil.value
assert result_pytz.value == 1382832000000000000

Expand Down Expand Up @@ -357,7 +361,6 @@ def test_astimezone(self, tzstr):

@td.skip_if_windows
def test_tz_convert_utc_with_system_utc(self):

# from system utc to real utc
ts = Timestamp("2001-01-05 11:56", tz=timezones.maybe_get_tz("dateutil/UTC"))
# check that the time hasn't changed.
Expand Down