diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 6ceae4dfd8a91..427b16cc02953 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -645,6 +645,7 @@ Deprecations - The ``inplace`` parameter of :meth:`Categorical.remove_categories`, :meth:`Categorical.add_categories`, :meth:`Categorical.reorder_categories`, :meth:`Categorical.rename_categories`, :meth:`Categorical.set_categories` is deprecated and will be removed in a future version (:issue:`37643`) - Deprecated :func:`merge` producing duplicated columns through the ``suffixes`` keyword and already existing columns (:issue:`22818`) - Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`) +- Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`) - Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 83946491f32a8..a366b49ce3c55 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2930,6 +2930,21 @@ def union(self, other, sort=None): "Can only union MultiIndex with MultiIndex or Index of tuples, " "try mi.to_flat_index().union(other) instead." ) + if ( + isinstance(self, ABCDatetimeIndex) + and isinstance(other, ABCDatetimeIndex) + and self.tz is not None + and other.tz is not None + ): + # GH#39328 + warnings.warn( + "In a future version, the union of DatetimeIndex objects " + "with mismatched timezones will cast both to UTC instead of " + "object dtype. To retain the old behavior, " + "use `index.astype(object).union(other)`", + FutureWarning, + stacklevel=2, + ) dtype = find_common_type([self.dtype, other.dtype]) if self._is_numeric_dtype and other._is_numeric_dtype: diff --git a/pandas/tests/indexes/datetimes/test_timezones.py b/pandas/tests/indexes/datetimes/test_timezones.py index bc01d44de0529..8bba11786e3e5 100644 --- a/pandas/tests/indexes/datetimes/test_timezones.py +++ b/pandas/tests/indexes/datetimes/test_timezones.py @@ -1146,7 +1146,10 @@ def test_dti_union_aware(self): rng2 = date_range("2012-11-15 12:00:00", periods=6, freq="H", tz="US/Eastern") - result = rng.union(rng2) + with tm.assert_produces_warning(FutureWarning): + # # GH#39328 will cast both to UTC + result = rng.union(rng2) + expected = rng.astype("O").union(rng2.astype("O")) tm.assert_index_equal(result, expected) assert result[0].tz.zone == "US/Central"