Skip to content

Commit e3143f6

Browse files
authored
BUG: when flooring, ambiguous parameter unnecessarily used (and raising Error) (#50024)
* fixed bug in tz_localize to throw an error if the argument to the ambiguos parameter is not one of the 4 correct options * fixed ambiguous check and timestamp test cases * Delete floor_timestamp_tests.py * fixed error in test cases * re-ran with pre-commit * merged with pandas/dev main branch and re-added previous changes * added quotes * added quotes to test case * added removed test case + whatsnew notes * fixed whatsnew note * removed notes from v1.5.3 and added to v2.0.0
1 parent 7b2dd38 commit e3143f6

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

doc/source/whatsnew/v1.5.3.rst

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Bug fixes
3636
Other
3737
~~~~~
3838
-
39-
-
4039

4140
.. ---------------------------------------------------------------------------
4241
.. _whatsnew_153.contributors:

doc/source/whatsnew/v2.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ Timezones
717717
^^^^^^^^^
718718
- 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`)
719719
- Bug in :func:`to_datetime` was failing to parse date strings with timezone name when ``format`` was specified with ``%Z`` (:issue:`49748`)
720-
-
720+
- Better error message when passing invalid values to ``ambiguous`` parameter in :meth:`Timestamp.tz_localize` (:issue:`49565`)
721721

722722
Numeric
723723
^^^^^^^

pandas/_libs/tslibs/timestamps.pyx

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ construction requirements, we need to do object instantiation in python
66
(see Timestamp class below). This will serve as a C extension type that
77
shadows the python class, where we do any heavy lifting.
88
"""
9-
import warnings
109

10+
import warnings
1111
cimport cython
1212

1313
import numpy as np
@@ -1944,8 +1944,11 @@ default 'raise'
19441944
>>> pd.NaT.tz_localize()
19451945
NaT
19461946
"""
1947-
if ambiguous == "infer":
1948-
raise ValueError("Cannot infer offset with only one time.")
1947+
if not isinstance(ambiguous, bool) and ambiguous not in {"NaT", "raise"}:
1948+
raise ValueError(
1949+
"'ambiguous' parameter must be one of: "
1950+
"True, False, 'NaT', 'raise' (default)"
1951+
)
19491952

19501953
nonexistent_options = ("raise", "NaT", "shift_forward", "shift_backward")
19511954
if nonexistent not in nonexistent_options and not PyDelta_Check(nonexistent):

pandas/tests/scalar/timestamp/test_timezones.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
datetime,
77
timedelta,
88
)
9+
import re
910

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

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

@@ -182,8 +186,8 @@ def test_tz_localize_ambiguous_compat(self):
182186

183187
pytz_zone = "Europe/London"
184188
dateutil_zone = "dateutil/Europe/London"
185-
result_pytz = naive.tz_localize(pytz_zone, ambiguous=0)
186-
result_dateutil = naive.tz_localize(dateutil_zone, ambiguous=0)
189+
result_pytz = naive.tz_localize(pytz_zone, ambiguous=False)
190+
result_dateutil = naive.tz_localize(dateutil_zone, ambiguous=False)
187191
assert result_pytz.value == result_dateutil.value
188192
assert result_pytz.value == 1382835600000000000
189193

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

196200
# 1 hour difference
197-
result_pytz = naive.tz_localize(pytz_zone, ambiguous=1)
198-
result_dateutil = naive.tz_localize(dateutil_zone, ambiguous=1)
201+
result_pytz = naive.tz_localize(pytz_zone, ambiguous=True)
202+
result_dateutil = naive.tz_localize(dateutil_zone, ambiguous=True)
199203
assert result_pytz.value == result_dateutil.value
200204
assert result_pytz.value == 1382832000000000000
201205

@@ -357,7 +361,6 @@ def test_astimezone(self, tzstr):
357361

358362
@td.skip_if_windows
359363
def test_tz_convert_utc_with_system_utc(self):
360-
361364
# from system utc to real utc
362365
ts = Timestamp("2001-01-05 11:56", tz=timezones.maybe_get_tz("dateutil/UTC"))
363366
# check that the time hasn't changed.

0 commit comments

Comments
 (0)