Skip to content

Commit 59feac7

Browse files
mroeschkejreback
authored andcommitted
ERR: Raise ValueError with naive datetime and mixed UTC offsets (#25982)
1 parent 6d51ea7 commit 59feac7

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ Timezones
283283
- Bug in :func:`Timestamp.tz_localize` and :func:`Timestamp.tz_convert` does not propagate ``freq`` (:issue:`25241`)
284284
- Bug in :func:`Series.at` where setting :class:`Timestamp` with timezone raises ``TypeError`` (:issue:`25506`)
285285
- Bug in :func:`DataFrame.update` when updating with timezone aware data would return timezone naive data (:issue:`25807`)
286+
- 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`)
286287

287288
Numeric
288289
^^^^^^^

pandas/_libs/tslib.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,8 @@ cdef array_to_datetime_object(ndarray[object] values, bint is_raise,
792792
# 2) datetime strings, which we return as datetime.datetime
793793
for i in range(n):
794794
val = values[i]
795-
if checknull_with_nat(val):
795+
if checknull_with_nat(val) or PyDateTime_Check(val):
796+
# GH 25978. No need to parse NaT-like or datetime-like vals
796797
oresult[i] = val
797798
elif isinstance(val, str):
798799
if len(val) == 0 or val in nat_strings:

pandas/tests/indexes/datetimes/test_tools.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ def test_iso_8601_strings_with_different_offsets(self):
762762
NaT], tz='UTC')
763763
tm.assert_index_equal(result, expected)
764764

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

788+
def test_mixed_offsets_with_native_datetime_raises(self):
789+
# GH 25978
790+
s = pd.Series([
791+
'nan',
792+
pd.Timestamp("1990-01-01"),
793+
"2015-03-14T16:15:14.123-08:00",
794+
"2019-03-04T21:56:32.620-07:00",
795+
None,
796+
])
797+
with pytest.raises(ValueError, match="Tz-aware datetime.datetime"):
798+
pd.to_datetime(s)
799+
788800
def test_non_iso_strings_with_tz_offset(self):
789801
result = to_datetime(['March 1, 2018 12:00:00+0400'] * 2)
790802
expected = DatetimeIndex([datetime(2018, 3, 1, 12,

0 commit comments

Comments
 (0)