diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index dacd433f112a5..976fe95d6ccd6 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -423,6 +423,7 @@ Reshaping - Bug in :func:`pandas.cut` where large bins could incorrectly raise an error due to an integer overflow (:issue:`26045`) - Bug in :func:`DataFrame.sort_index` where an error is thrown when a multi-indexed DataFrame is sorted on all levels with the initial level sorted last (:issue:`26053`) - Bug in :meth:`Series.nlargest` treats ``True`` as smaller than ``False`` (:issue:`26154`) +- Bug in :meth:`Series.combine_first` where combining a :class:`Series` with an :class:`Index` with mixed timezones would raise a ``ValueError`` (:issue:`26283`) Sparse ^^^^^^ diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 6b9806f4d32f5..af4d66b1fe5d7 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -489,6 +489,9 @@ def union(self, other, sort=None): other = DatetimeIndex(other) except TypeError: pass + except ValueError: + # GH 26283: Convert indexes with mixed tz to UTC + other = tools.to_datetime(other, utc=True) this, other = self._maybe_utc_convert(other) diff --git a/pandas/tests/series/test_combine_concat.py b/pandas/tests/series/test_combine_concat.py index 41c3e220ad06f..6ea80f7821f0d 100644 --- a/pandas/tests/series/test_combine_concat.py +++ b/pandas/tests/series/test_combine_concat.py @@ -5,7 +5,7 @@ import pytest import pandas as pd -from pandas import DataFrame, DatetimeIndex, Series, date_range +from pandas import DataFrame, DatetimeIndex, Series, Timestamp, date_range import pandas.util.testing as tm from pandas.util.testing import assert_frame_equal, assert_series_equal @@ -368,3 +368,23 @@ def test_append_concat_tz_dateutil(self): appended = rng.append(rng2) tm.assert_index_equal(appended, rng3) + + def test_combine_first_with_mixed_tz(self): + # GH 26283 + uniform_tz = Series({Timestamp("2019-05-01", tz='UTC'): 1.0}) + + multi_tz = Series( + { + Timestamp("2019-05-01 01:00:00+0100", tz='Europe/London'): 2.0, + Timestamp("2019-05-02", tz='UTC'): 3.0 + } + ) + + result = uniform_tz.combine_first(multi_tz) + expected = Series( + { + Timestamp("2019-05-01", tz='UTC'): 1.0, + Timestamp("2019-05-02", tz='UTC'): 3.0 + } + ) + tm.assert_series_equal(result, expected)