From 58f3afc5dd8a071f5222fdb4a7149db64ae1caef Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 30 Sep 2022 04:48:52 -0700 Subject: [PATCH] BUG: to_datetime(tz_mix, utc=True) converts to UTC (#48686) Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com> --- doc/source/whatsnew/v1.5.1.rst | 1 + pandas/_libs/tslib.pyx | 2 +- pandas/tests/tools/test_to_datetime.py | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index 32c946af1422f..4c4595e8244fb 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -72,6 +72,7 @@ Fixed regressions - Fixed Regression in :meth:`Series.__setitem__` casting ``None`` to ``NaN`` for object dtype (:issue:`48665`) - Fixed Regression in :meth:`DataFrame.loc` when setting values as a :class:`DataFrame` with all ``True`` indexer (:issue:`48701`) - 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`) - Fixed regression in :meth:`DataFrame.loc` raising ``FutureWarning`` when setting an empty :class:`DataFrame` (:issue:`48480`) - Fixed regression in :meth:`DataFrame.describe` raising ``TypeError`` when result contains ``NA`` (:issue:`48778`) - Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index a98027b9153fa..3526ea3438aff 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 89f79f0072951..7db740e36d3a9 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -2817,6 +2817,30 @@ def test_to_datetime_cache_coerce_50_lines_outofbounds(series_length): 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) + + def test_to_datetime_format_f_parse_nanos(): # GH 48767 timestamp = "15/02/2020 02:03:04.123456789"