Skip to content

Commit 017bde1

Browse files
authored
BUG: date_range doesn't propagate ambigous=False to tz_localize (#35302)
1 parent 52e815f commit 017bde1

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Timedelta
7171
Timezones
7272
^^^^^^^^^
7373

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

7777

pandas/core/arrays/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ def _generate_range(
418418
# index is localized datetime64 array -> have to convert
419419
# start/end as well to compare
420420
if start is not None:
421-
start = start.tz_localize(tz).asm8
421+
start = start.tz_localize(tz, ambiguous, nonexistent).asm8
422422
if end is not None:
423-
end = end.tz_localize(tz).asm8
423+
end = end.tz_localize(tz, ambiguous, nonexistent).asm8
424424
else:
425425
# Create a linearly spaced date_range in local time
426426
# Nanosecond-granularity timestamps aren't always correctly

pandas/tests/indexes/datetimes/test_constructors.py

+59
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,65 @@ def test_construction_with_nat_and_tzlocal(self):
787787
expected = DatetimeIndex([Timestamp("2018", tz=tz), pd.NaT])
788788
tm.assert_index_equal(result, expected)
789789

790+
def test_constructor_with_ambiguous_keyword_arg(self):
791+
# GH 35297
792+
793+
expected = DatetimeIndex(
794+
["2020-11-01 01:00:00", "2020-11-02 01:00:00"],
795+
dtype="datetime64[ns, America/New_York]",
796+
freq="D",
797+
ambiguous=False,
798+
)
799+
800+
# ambiguous keyword in start
801+
timezone = "America/New_York"
802+
start = pd.Timestamp(year=2020, month=11, day=1, hour=1).tz_localize(
803+
timezone, ambiguous=False
804+
)
805+
result = pd.date_range(start=start, periods=2, ambiguous=False)
806+
tm.assert_index_equal(result, expected)
807+
808+
# ambiguous keyword in end
809+
timezone = "America/New_York"
810+
end = pd.Timestamp(year=2020, month=11, day=2, hour=1).tz_localize(
811+
timezone, ambiguous=False
812+
)
813+
result = pd.date_range(end=end, periods=2, ambiguous=False)
814+
tm.assert_index_equal(result, expected)
815+
816+
def test_constructor_with_nonexistent_keyword_arg(self):
817+
# GH 35297
818+
819+
timezone = "Europe/Warsaw"
820+
821+
# nonexistent keyword in start
822+
start = pd.Timestamp("2015-03-29 02:30:00").tz_localize(
823+
timezone, nonexistent="shift_forward"
824+
)
825+
result = pd.date_range(start=start, periods=2, freq="H")
826+
expected = DatetimeIndex(
827+
[
828+
pd.Timestamp("2015-03-29 03:00:00+02:00", tz=timezone),
829+
pd.Timestamp("2015-03-29 04:00:00+02:00", tz=timezone),
830+
]
831+
)
832+
833+
tm.assert_index_equal(result, expected)
834+
835+
# nonexistent keyword in end
836+
end = pd.Timestamp("2015-03-29 02:30:00").tz_localize(
837+
timezone, nonexistent="shift_forward"
838+
)
839+
result = pd.date_range(end=end, periods=2, freq="H")
840+
expected = DatetimeIndex(
841+
[
842+
pd.Timestamp("2015-03-29 01:00:00+01:00", tz=timezone),
843+
pd.Timestamp("2015-03-29 03:00:00+02:00", tz=timezone),
844+
]
845+
)
846+
847+
tm.assert_index_equal(result, expected)
848+
790849
def test_constructor_no_precision_raises(self):
791850
# GH-24753, GH-24739
792851

0 commit comments

Comments
 (0)