diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 8dabaeb6c7bfe..4de9854330bc0 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -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 ^^^^^^^ diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 5f0c2c9659b35..d0ae15e28bb4d 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -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: diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index 4d4ad151173b1..eaf689cfa1c21 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -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', @@ -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,