Skip to content

Commit c7e36af

Browse files
Backport PR #48686 on branch 1.5.x (BUG: to_datetime(tz_mix, utc=True) converts to UTC) (#48882)
Backport PR #48686: BUG: to_datetime(tz_mix, utc=True) converts to UTC Co-authored-by: Matthew Roeschke <[email protected]>
1 parent c91e603 commit c7e36af

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Fixed regressions
7272
- Fixed Regression in :meth:`Series.__setitem__` casting ``None`` to ``NaN`` for object dtype (:issue:`48665`)
7373
- Fixed Regression in :meth:`DataFrame.loc` when setting values as a :class:`DataFrame` with all ``True`` indexer (:issue:`48701`)
7474
- Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`)
75+
- Regression in :func:`to_datetime` when ``utc=True`` and ``arg`` contained timezone naive and aware arguments raised a ``ValueError`` (:issue:`48678`)
7576
- Fixed regression in :meth:`DataFrame.loc` raising ``FutureWarning`` when setting an empty :class:`DataFrame` (:issue:`48480`)
7677
- Fixed regression in :meth:`DataFrame.describe` raising ``TypeError`` when result contains ``NA`` (:issue:`48778`)
7778
- Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`)

pandas/_libs/tslib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ cpdef array_to_datetime(
533533

534534
else:
535535
found_naive = True
536-
if found_tz:
536+
if found_tz and not utc_convert:
537537
raise ValueError('Cannot mix tz-aware with '
538538
'tz-naive values')
539539
if isinstance(val, _Timestamp):

pandas/tests/tools/test_to_datetime.py

+24
Original file line numberDiff line numberDiff line change
@@ -2817,6 +2817,30 @@ def test_to_datetime_cache_coerce_50_lines_outofbounds(series_length):
28172817
to_datetime(s, errors="raise", utc=True)
28182818

28192819

2820+
@pytest.mark.parametrize(
2821+
"arg",
2822+
[
2823+
["1724-12-20 20:20:20+00:00", "2022-01-01 00:00:00"],
2824+
[
2825+
Timestamp("1724-12-20 20:20:20+00:00"),
2826+
Timestamp("2022-01-01 00:00:00"),
2827+
],
2828+
[datetime(1724, 12, 20, 20, 20, 20, tzinfo=timezone.utc), datetime(2022, 1, 1)],
2829+
],
2830+
ids=["string", "pd.Timestamp", "datetime.datetime"],
2831+
)
2832+
@pytest.mark.parametrize("tz_aware_first", [True, False])
2833+
def test_to_datetime_mixed_tzaware_timestamp_utc_true(arg, tz_aware_first):
2834+
# GH 48678
2835+
exp_arg = ["1724-12-20 20:20:20", "2022-01-01 00:00:00"]
2836+
if not tz_aware_first:
2837+
arg.reverse()
2838+
exp_arg.reverse()
2839+
result = to_datetime(arg, utc=True)
2840+
expected = DatetimeIndex(exp_arg).tz_localize("UTC")
2841+
tm.assert_index_equal(result, expected)
2842+
2843+
28202844
def test_to_datetime_format_f_parse_nanos():
28212845
# GH 48767
28222846
timestamp = "15/02/2020 02:03:04.123456789"

0 commit comments

Comments
 (0)