Skip to content
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

BUG: to_datetime(tz_mix, utc=True) converts to UTC #48686

Merged
merged 8 commits into from
Sep 30, 2022
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down