Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98113f3

Browse files
committed
BUG: Fix naive timestamps inheriting timezone from previous timestamps in to_datetime with ISO8601 format
1 parent 5736b96 commit 98113f3

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ Datetimelike
658658
- Bug in :meth:`to_datetime` on float array with missing values throwing ``FloatingPointError`` (:issue:`58419`)
659659
- Bug in :meth:`to_datetime` on float32 df with year, month, day etc. columns leads to precision issues and incorrect result. (:issue:`60506`)
660660
- Bug in :meth:`to_datetime` reports incorrect index in case of any failure scenario. (:issue:`58298`)
661+
- Bug in :meth:`to_datetime` with ``format="ISO8601"`` and ``utc=True`` where naive timestamps incorrectly inherited timezone offset from previous timestamps in a series. (:issue:`61389`)
661662
- Bug in :meth:`to_datetime` wrongly converts when ``arg`` is a ``np.datetime64`` object with unit of ``ps``. (:issue:`60341`)
662663
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)
663664

pandas/_libs/tslibs/strptime.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ def array_strptime(
444444
else:
445445
val = str(val)
446446

447+
out_local = 0
448+
out_tzoffset = 0
449+
447450
if fmt == "ISO8601":
448451
string_to_dts_succeeded = not string_to_dts(
449452
val, &dts, &out_bestunit, &out_local,

pandas/tests/tools/test_to_datetime.py

+48
Original file line numberDiff line numberDiff line change
@@ -3514,6 +3514,54 @@ def test_to_datetime_mixed_not_necessarily_iso8601_coerce():
35143514
tm.assert_index_equal(result, DatetimeIndex(["2020-01-01 00:00:00", NaT]))
35153515

35163516

3517+
def test_to_datetime_iso8601_utc_single_naive():
3518+
# GH#61389
3519+
result = to_datetime("2023-10-15T14:30:00", utc=True, format="ISO8601")
3520+
expected = Timestamp("2023-10-15 14:30:00+00:00")
3521+
assert result == expected
3522+
3523+
3524+
def test_to_datetime_iso8601_utc_mixed_negative_offset():
3525+
# GH#61389
3526+
data = ["2023-10-15T10:30:00-12:00", "2023-10-15T14:30:00"]
3527+
result = to_datetime(data, utc=True, format="ISO8601")
3528+
3529+
expected = DatetimeIndex(
3530+
[Timestamp("2023-10-15 22:30:00+00:00"), Timestamp("2023-10-15 14:30:00+00:00")]
3531+
)
3532+
tm.assert_index_equal(result, expected)
3533+
3534+
3535+
def test_to_datetime_iso8601_utc_mixed_positive_offset():
3536+
# GH#61389
3537+
data = ["2023-10-15T10:30:00+08:00", "2023-10-15T14:30:00"]
3538+
result = to_datetime(data, utc=True, format="ISO8601")
3539+
3540+
expected = DatetimeIndex(
3541+
[Timestamp("2023-10-15 02:30:00+00:00"), Timestamp("2023-10-15 14:30:00+00:00")]
3542+
)
3543+
tm.assert_index_equal(result, expected)
3544+
3545+
3546+
def test_to_datetime_iso8601_utc_mixed_both_offsets():
3547+
# GH#61389
3548+
data = [
3549+
"2023-10-15T10:30:00+08:00",
3550+
"2023-10-15T12:30:00-05:00",
3551+
"2023-10-15T14:30:00",
3552+
]
3553+
result = to_datetime(data, utc=True, format="ISO8601")
3554+
3555+
expected = DatetimeIndex(
3556+
[
3557+
Timestamp("2023-10-15 02:30:00+00:00"),
3558+
Timestamp("2023-10-15 17:30:00+00:00"),
3559+
Timestamp("2023-10-15 14:30:00+00:00"),
3560+
]
3561+
)
3562+
tm.assert_index_equal(result, expected)
3563+
3564+
35173565
def test_unknown_tz_raises():
35183566
# GH#18702, GH#51476
35193567
dtstr = "2014 Jan 9 05:15 FAKE"

0 commit comments

Comments
 (0)