Skip to content

ERR: Raise ValueError with naive datetime and mixed UTC offsets #25982

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 5 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ Timezones
- Bug in :func:`Timestamp.tz_localize` and :func:`Timestamp.tz_convert` does not propagate ``freq`` (:issue:`25241`)
- Bug in :func:`Series.at` where setting :class:`Timestamp` with timezone raises ``TypeError`` (:issue:`25506`)
- Bug in :func:`DataFrame.update` when updating with timezone aware data would return timezone naive data (:issue:`25807`)
- Bug in :func:`to_datetime` where an uninformative ``RuntimeError`` was raised when passing a naive :class:`Timestamp` with datetime strings with mixed UTC offsets (:issue:`25978`)

Numeric
^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,8 @@ cdef array_to_datetime_object(ndarray[object] values, bint is_raise,
# 2) datetime strings, which we return as datetime.datetime
for i in range(n):
val = values[i]
if checknull_with_nat(val):
if checknull_with_nat(val) or PyDateTime_Check(val):
# GH 25978. No need to parse NaT-like or datetime-like vals
oresult[i] = val
elif isinstance(val, str):
if len(val) == 0 or val in nat_strings:
Expand Down
14 changes: 13 additions & 1 deletion pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def test_iso_8601_strings_with_different_offsets(self):
NaT], tz='UTC')
tm.assert_index_equal(result, expected)

def test_iss8601_strings_mixed_offsets_with_naive(self):
def test_iso8601_strings_mixed_offsets_with_naive(self):
# GH 24992
result = pd.to_datetime([
'2018-11-28T00:00:00',
Expand All @@ -785,6 +785,18 @@ def test_iss8601_strings_mixed_offsets_with_naive(self):
expected = pd.to_datetime(list(reversed(items)), utc=True)[::-1]
tm.assert_index_equal(result, expected)

def test_mixed_offsets_with_native_datetime_raises(self):
# GH 25978
s = pd.Series([
'nan',
pd.Timestamp("1990-01-01"),
"2015-03-14T16:15:14.123-08:00",
"2019-03-04T21:56:32.620-07:00",
None,
])
with pytest.raises(ValueError, match="Tz-aware datetime.datetime"):
pd.to_datetime(s)

def test_non_iso_strings_with_tz_offset(self):
result = to_datetime(['March 1, 2018 12:00:00+0400'] * 2)
expected = DatetimeIndex([datetime(2018, 3, 1, 12,
Expand Down