Skip to content

BUG: AmbiguousTimeError when using date_range even when argument ambiguous is set #35297

Closed
@aleisun

Description

@aleisun
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

>>> import pandas as pd
>>> timezone = 'America/New_York'
>>> start = pd.Timestamp(year=2020, month=11, day=1, hour=1).tz_localize(timezone, ambiguous=False)
>>> pd.date_range(start, periods=2, ambiguous=False)

---------------------------------------------------------------------------
AmbiguousTimeError                        Traceback (most recent call last)
<ipython-input-5-d142fd70e406> in <module>
      2 timezone = 'America/New_York'
      3 start = pd.Timestamp(year=2020, month=11, day=1, hour=1).tz_localize(timezone, ambiguous=False)
----> 4 pd.date_range(start, periods=2, ambiguous=False)

~/anaconda3/envs/oids_tst/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py in date_range(start, end, periods, freq, tz, normalize, name, closed, **kwargs)
   1178         normalize=normalize,
   1179         closed=closed,
-> 1180         **kwargs,
   1181     )
   1182     return DatetimeIndex._simple_new(dtarr, tz=dtarr.tz, freq=dtarr.freq, name=name)

~/anaconda3/envs/oids_tst/lib/python3.6/site-packages/pandas/core/arrays/datetimes.py in _generate_range(cls, start, end, periods, freq, tz, normalize, ambiguous, nonexistent, closed)
    419                 # start/end as well to compare
    420                 if start is not None:
--> 421                     start = start.tz_localize(tz).asm8
    422                 if end is not None:
    423                     end = end.tz_localize(tz).asm8

pandas/_libs/tslibs/timestamps.pyx in pandas._libs.tslibs.timestamps.Timestamp.tz_localize()

pandas/_libs/tslibs/tzconversion.pyx in pandas._libs.tslibs.tzconversion.tz_localize_to_utc()

AmbiguousTimeError: Cannot infer dst time from 2020-11-01 01:00:00, try using the 'ambiguous' argument

Problem description

When the start time is at the day light saving ambiguous time, date_range raises errors even when the argument 'ambiguous' is set. It should not be raising error when the argument is set.

Issue could be due to the line

start = start.tz_localize(tz).asm8

does not correctly pass the argument ambiguous to the function tz_localize().

Expected Output

No error

Output of pd.show_versions()

AmbiguousTimeError: Cannot infer dst time from 2020-11-01 01:00:00, try using the 'ambiguous' argument

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions