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 13 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
5 changes: 3 additions & 2 deletions doc/source/whatsnew/v1.5.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@ Fixed regressions
Bug fixes
~~~~~~~~~
- Bug in :meth:`.Styler.to_excel` leading to error when unrecognized ``border-style`` (e.g. ``"hair"``) provided to Excel writers (:issue:`48649`)
-
- Bug in :meth:`.Timestamp.floor` fixed tz_localize so 'ambiguous' parameter can only be True, False, 'NaT' or 'raise' and all other invalid arguments to ambiguous have same error, including 'infer' (:issue:`49565`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should go in v2.0.0.rst

as for the test, how about

Better error message when passing invalid values to ``ambiguous`` parameter in :meth:`Timestamp.tz_localize` (:issue:`49565`)


.. ---------------------------------------------------------------------------
.. _whatsnew_153.other:

Other
~~~~~
-
- Modified test cases in test_timezones.py to reflect new error message from bug fix for bug: (:issue:`49565`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unnecessary, let's remove it

-

.. ---------------------------------------------------------------------------
.. _whatsnew_153.contributors:

Contributors
~~~~~~~~~~~~
- Julia Chaker Aoun
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need, this is populated automatically

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! If you could approve or merge my PR by tomorrow I would really appreciate it! I'm working on this for a project and I get extra credit if the PR is approved :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll need to address the review comments first

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I resolved the remaining comments in my last commit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still need to address #50024 and #50024


.. contributors:: v1.5.2..v1.5.3|HEAD
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
18 changes: 9 additions & 9 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 All @@ -368,9 +371,6 @@ def test_tz_convert_utc_with_system_utc(self):
# check that the time hasn't changed.
assert ts == ts.tz_convert(dateutil.tz.tzutc())

# ------------------------------------------------------------------
# Timestamp.__init__ with tz str or tzinfo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you check over the file again please? let's not make irrelevant changes

def test_timestamp_constructor_tz_utc(self):
utc_stamp = Timestamp("3/11/2012 05:00", tz="utc")
assert utc_stamp.tzinfo is pytz.utc
Expand Down