Skip to content

BUG: date_range doesn't propagate ambigous=False to tz_localize #35302

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
Aug 1, 2020
Merged
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Timedelta
Timezones
^^^^^^^^^

-
- Bug in :func:`date_range` was raising AmbiguousTimeError for valid input with `ambiguous=False` (:issue:`35297`)
-


Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ def _generate_range(
# index is localized datetime64 array -> have to convert
# start/end as well to compare
if start is not None:
start = start.tz_localize(tz).asm8
start = start.tz_localize(tz, ambiguous, nonexistent).asm8
if end is not None:
end = end.tz_localize(tz).asm8
end = end.tz_localize(tz, ambiguous, nonexistent).asm8
else:
# Create a linearly spaced date_range in local time
# Nanosecond-granularity timestamps aren't always correctly
Expand Down
59 changes: 59 additions & 0 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,65 @@ def test_construction_with_nat_and_tzlocal(self):
expected = DatetimeIndex([Timestamp("2018", tz=tz), pd.NaT])
tm.assert_index_equal(result, expected)

def test_constructor_with_ambiguous_keyword_arg(self):
# GH 35297

expected = DatetimeIndex(
["2020-11-01 01:00:00", "2020-11-02 01:00:00"],
dtype="datetime64[ns, America/New_York]",
freq="D",
ambiguous=False,
)

# ambiguous keyword in start
timezone = "America/New_York"
start = pd.Timestamp(year=2020, month=11, day=1, hour=1).tz_localize(
timezone, ambiguous=False
)
result = pd.date_range(start=start, periods=2, ambiguous=False)
tm.assert_index_equal(result, expected)

# ambiguous keyword in end
timezone = "America/New_York"
end = pd.Timestamp(year=2020, month=11, day=2, hour=1).tz_localize(
timezone, ambiguous=False
)
result = pd.date_range(end=end, periods=2, ambiguous=False)
tm.assert_index_equal(result, expected)

def test_constructor_with_nonexistent_keyword_arg(self):
# GH 35297

timezone = "Europe/Warsaw"

# nonexistent keyword in start
start = pd.Timestamp("2015-03-29 02:30:00").tz_localize(
timezone, nonexistent="shift_forward"
)
result = pd.date_range(start=start, periods=2, freq="H")
expected = DatetimeIndex(
[
pd.Timestamp("2015-03-29 03:00:00+02:00", tz=timezone),
pd.Timestamp("2015-03-29 04:00:00+02:00", tz=timezone),
]
)

tm.assert_index_equal(result, expected)

# nonexistent keyword in end
end = pd.Timestamp("2015-03-29 02:30:00").tz_localize(
timezone, nonexistent="shift_forward"
)
result = pd.date_range(end=end, periods=2, freq="H")
expected = DatetimeIndex(
[
pd.Timestamp("2015-03-29 01:00:00+01:00", tz=timezone),
pd.Timestamp("2015-03-29 03:00:00+02:00", tz=timezone),
]
)

tm.assert_index_equal(result, expected)

def test_constructor_no_precision_raises(self):
# GH-24753, GH-24739

Expand Down