Skip to content

AmbiguousTimeError in date_range using tz when start/end in dst transition. #23513

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

Closed
Safrone opened this issue Nov 5, 2018 · 3 comments
Closed
Labels
Datetime Datetime data dtype Numeric Operations Arithmetic, Comparison, and Logical operations

Comments

@Safrone
Copy link

Safrone commented Nov 5, 2018

Code Sample

import datetime
import pytz
import pandas as pd
start = datetime.datetime(2018, 11, 1, 1, 0)
end = datetime.datetime(2018, 11, 4, 1, 0)
tz = pytz.timezone('America/New_York')

pd.date_range(start, end, tz=tz, ambiguous=True)
Traceback (most recent call last):
  File "/venv/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 3265, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-40-6d5bf1272830>", line 1, in <module>
    pd.date_range(start, end, tz=self.timezone, ambiguous=False)
  File "/venv/lib/python3.5/site-packages/pandas/core/indexes/datetimes.py", line 2749, in date_range
    closed=closed, **kwargs)
  File "/venv/lib/python3.5/site-packages/pandas/core/indexes/datetimes.py", line 381, in __new__
    ambiguous=ambiguous)
  File "/venv/lib/python3.5/site-packages/pandas/core/indexes/datetimes.py", line 587, in _generate
    end = end.tz_localize(tz).asm8
  File "pandas/_libs/tslibs/timestamps.pyx", line 903, in pandas._libs.tslibs.timestamps.Timestamp.tz_localize
  File "pandas/_libs/tslibs/conversion.pyx", line 963, in pandas._libs.tslibs.conversion.tz_localize_to_utc
pytz.exceptions.AmbiguousTimeError: Cannot infer dst time from '2018-11-04 01:00:00', try using the 'ambiguous' argument

Problem description

pd.date_range does not honor the ambiguous keyword

Expected Output

DatetimeIndex(['2018-11-01 01:00:00-04:00', '2018-11-02 01:00:00-04:00',
               '2018-11-03 01:00:00-04:00', '2018-11-04 01:00:00-04:00'],
              dtype='datetime64[ns, America/New_York]', freq='D')

or

DatetimeIndex(['2018-11-01 01:00:00-04:00', '2018-11-02 01:00:00-04:00',
               '2018-11-03 01:00:00-04:00', '2018-11-04 01:00:00-05:00'],
              dtype='datetime64[ns, America/New_York]', freq='D')

depending on the truth value of ambiguous

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.6.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-38-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.23.4
pytest: None
pip: 10.0.1
setuptools: 39.1.0
Cython: None
numpy: 1.13.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.0.1
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: 4.2.5
bs4: 4.6.3
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@gfyoung
Copy link
Member

gfyoung commented Nov 5, 2018

cc @mroeschke

I believe we might have fixed this in #22647?

@Safrone : Thanks for filing this! Can you installing the master branch an re-running your code?

@gfyoung gfyoung added Datetime Datetime data dtype Numeric Operations Arithmetic, Comparison, and Logical operations labels Nov 5, 2018
@Safrone
Copy link
Author

Safrone commented Nov 5, 2018

With the latest branch:

import datetime
import pytz
import pandas as pd
print(pd.__version__)

0.24.0.dev0+917.gf0877eccc

start = datetime.datetime(2018, 11, 1, 1, 0)
end = datetime.datetime(2018, 11, 4, 1, 0)
tz = pytz.timezone('America/New_York')
dr = pd.date_range(start, end, tz=tz, ambiguous=True)
print(dr)
DatetimeIndex(['2018-11-01 01:00:00-04:00', '2018-11-02 01:00:00-04:00',
               '2018-11-03 01:00:00-04:00', '2018-11-04 01:00:00-04:00'],
              dtype='datetime64[ns, America/New_York]', freq='D')

Thanks!

@mroeschke
Copy link
Member

Glad it's working for you on master (I believe this was fixed recently). This will be apart of the v0.24.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

No branches or pull requests

3 participants