Skip to content

BUG: Fix naive timestamps inheriting timezone from previous timestamps in to_datetime with ISO8601 format #61400

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 1 commit into from
May 6, 2025
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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ Datetimelike
- Bug in :meth:`to_datetime` on float array with missing values throwing ``FloatingPointError`` (:issue:`58419`)
- Bug in :meth:`to_datetime` on float32 df with year, month, day etc. columns leads to precision issues and incorrect result. (:issue:`60506`)
- Bug in :meth:`to_datetime` reports incorrect index in case of any failure scenario. (:issue:`58298`)
- 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`)
- Bug in :meth:`to_datetime` wrongly converts when ``arg`` is a ``np.datetime64`` object with unit of ``ps``. (:issue:`60341`)
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)

Expand Down
3 changes: 3 additions & 0 deletions pandas/_libs/tslibs/strptime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ def array_strptime(
else:
val = str(val)

out_local = 0
out_tzoffset = 0

if fmt == "ISO8601":
string_to_dts_succeeded = not string_to_dts(
val, &dts, &out_bestunit, &out_local,
Expand Down
48 changes: 48 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3514,6 +3514,54 @@ def test_to_datetime_mixed_not_necessarily_iso8601_coerce():
tm.assert_index_equal(result, DatetimeIndex(["2020-01-01 00:00:00", NaT]))


def test_to_datetime_iso8601_utc_single_naive():
# GH#61389
result = to_datetime("2023-10-15T14:30:00", utc=True, format="ISO8601")
expected = Timestamp("2023-10-15 14:30:00+00:00")
assert result == expected


def test_to_datetime_iso8601_utc_mixed_negative_offset():
# GH#61389
data = ["2023-10-15T10:30:00-12:00", "2023-10-15T14:30:00"]
result = to_datetime(data, utc=True, format="ISO8601")

expected = DatetimeIndex(
[Timestamp("2023-10-15 22:30:00+00:00"), Timestamp("2023-10-15 14:30:00+00:00")]
)
tm.assert_index_equal(result, expected)


def test_to_datetime_iso8601_utc_mixed_positive_offset():
# GH#61389
data = ["2023-10-15T10:30:00+08:00", "2023-10-15T14:30:00"]
result = to_datetime(data, utc=True, format="ISO8601")

expected = DatetimeIndex(
[Timestamp("2023-10-15 02:30:00+00:00"), Timestamp("2023-10-15 14:30:00+00:00")]
)
tm.assert_index_equal(result, expected)


def test_to_datetime_iso8601_utc_mixed_both_offsets():
# GH#61389
data = [
"2023-10-15T10:30:00+08:00",
"2023-10-15T12:30:00-05:00",
"2023-10-15T14:30:00",
]
result = to_datetime(data, utc=True, format="ISO8601")

expected = DatetimeIndex(
[
Timestamp("2023-10-15 02:30:00+00:00"),
Timestamp("2023-10-15 17:30:00+00:00"),
Timestamp("2023-10-15 14:30:00+00:00"),
]
)
tm.assert_index_equal(result, expected)


def test_unknown_tz_raises():
# GH#18702, GH#51476
dtstr = "2014 Jan 9 05:15 FAKE"
Expand Down
Loading