From a39ae7f285feb64aaa9fba199944b6d84c9a1cd8 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 21 Sep 2022 09:59:47 -0700 Subject: [PATCH] BUG: to_datetime(tz_mix, utc=True) converts to UTC --- doc/source/whatsnew/v1.5.1.rst | 2 +- pandas/_libs/tslib.pyx | 2 +- pandas/tests/tools/test_to_datetime.py | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index 9d40d9118db32..a308c3d89ad1a 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -15,7 +15,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ - Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`) -- +- Regression in :func:`to_datetime` when ``utc=True`` and ``arg`` contained timezone naive and aware arguments raised a ``ValueError`` (:issue:`48678`) .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 4d071793b3935..8a21711a86e05 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -531,7 +531,7 @@ cpdef array_to_datetime( else: found_naive = True - if found_tz: + if found_tz and not utc_convert: raise ValueError('Cannot mix tz-aware with ' 'tz-naive values') if isinstance(val, _Timestamp): diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 7e698b7a6b83d..590e506fca899 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -2815,3 +2815,27 @@ def test_to_datetime_cache_coerce_50_lines_outofbounds(series_length): with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond timestamp"): to_datetime(s, errors="raise", utc=True) + + +@pytest.mark.parametrize( + "arg", + [ + ["1724-12-20 20:20:20+00:00", "2022-01-01 00:00:00"], + [ + Timestamp("1724-12-20 20:20:20+00:00"), + Timestamp("2022-01-01 00:00:00"), + ], + [datetime(1724, 12, 20, 20, 20, 20, tzinfo=timezone.utc), datetime(2022, 1, 1)], + ], + ids=["string", "pd.Timestamp", "datetime.datetime"], +) +@pytest.mark.parametrize("tz_aware_first", [True, False]) +def test_to_datetime_mixed_tzaware_timestamp_utc_true(arg, tz_aware_first): + # GH 48678 + exp_arg = ["1724-12-20 20:20:20", "2022-01-01 00:00:00"] + if not tz_aware_first: + arg.reverse() + exp_arg.reverse() + result = to_datetime(arg, utc=True) + expected = DatetimeIndex(exp_arg).tz_localize("UTC") + tm.assert_index_equal(result, expected)