Skip to content

AmbiguousTimeError in floor() operation #18946

Closed
@tapia

Description

@tapia

This is probably related to #18885

Given a DataFrame with a DST change on it:

df1=pd.DataFrame([pd.to_datetime('2017-10-29 02:00:00+02:00'),
                  pd.to_datetime('2017-10-29 02:00:00+01:00'),
                  pd.to_datetime('2017-10-29 03:00:00+01:00')],columns=['date'])
df1['date'] = df1['date'].dt.tz_localize('UTC').dt.tz_convert('Europe/Madrid')
df1['value'] = 1

When we try to do a floor() or ceil() operation, we get an AmbiguousTimeError exception:

df1.date.dt.floor('H')

Expected output

0   2017-10-29 02:00:00+02:00
1   2017-10-29 02:00:00+01:00
2   2017-10-29 03:00:00+01:00

Actual output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "(...)/venv/lib/python3.6/site-packages/pandas/core/accessor.py", line 115, in f
    return self._delegate_method(name, *args, **kwargs)
  File "(...)/venv/lib/python3.6/site-packages/pandas/core/indexes/accessors.py", line 131, in _delegate_method
    result = method(*args, **kwargs)
  File "(...)/venv/lib/python3.6/site-packages/pandas/core/indexes/datetimelike.py", line 118, in floor
    return self._round(freq, np.floor)
  File "(...)/venv/lib/python3.6/site-packages/pandas/core/indexes/datetimelike.py", line 110, in _round
    self._shallow_copy(result, **attribs))
  File "(...)/venv/lib/python3.6/site-packages/pandas/core/indexes/datetimelike.py", line 230, in _ensure_localized
    result = result.tz_localize(self.tz)
  File "(...)/venv/lib/python3.6/site-packages/pandas/util/_decorators.py", line 118, in wrapper
    return func(*args, **kwargs)
  File "(...)/venv/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py", line 1858, in tz_localize
    errors=errors)
  File "pandas/_libs/tslib.pyx", line 3593, in pandas._libs.tslib.tz_localize_to_utc
pytz.exceptions.AmbiguousTimeError: Cannot infer dst time from Timestamp('2017-10-29 02: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