Skip to content

Commit e0ca3de

Browse files
authored
DEPR: DatetimeIndex.union with mixed timezones (#41458)
1 parent ac026fa commit e0ca3de

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ Deprecations
645645
- 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`)
646646
- Deprecated :func:`merge` producing duplicated columns through the ``suffixes`` keyword and already existing columns (:issue:`22818`)
647647
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
648+
- 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`)
648649
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
649650

650651
.. ---------------------------------------------------------------------------

pandas/core/indexes/base.py

+15
Original file line numberDiff line numberDiff line change
@@ -2930,6 +2930,21 @@ def union(self, other, sort=None):
29302930
"Can only union MultiIndex with MultiIndex or Index of tuples, "
29312931
"try mi.to_flat_index().union(other) instead."
29322932
)
2933+
if (
2934+
isinstance(self, ABCDatetimeIndex)
2935+
and isinstance(other, ABCDatetimeIndex)
2936+
and self.tz is not None
2937+
and other.tz is not None
2938+
):
2939+
# GH#39328
2940+
warnings.warn(
2941+
"In a future version, the union of DatetimeIndex objects "
2942+
"with mismatched timezones will cast both to UTC instead of "
2943+
"object dtype. To retain the old behavior, "
2944+
"use `index.astype(object).union(other)`",
2945+
FutureWarning,
2946+
stacklevel=2,
2947+
)
29332948

29342949
dtype = find_common_type([self.dtype, other.dtype])
29352950
if self._is_numeric_dtype and other._is_numeric_dtype:

pandas/tests/indexes/datetimes/test_timezones.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,10 @@ def test_dti_union_aware(self):
11461146

11471147
rng2 = date_range("2012-11-15 12:00:00", periods=6, freq="H", tz="US/Eastern")
11481148

1149-
result = rng.union(rng2)
1149+
with tm.assert_produces_warning(FutureWarning):
1150+
# # GH#39328 will cast both to UTC
1151+
result = rng.union(rng2)
1152+
11501153
expected = rng.astype("O").union(rng2.astype("O"))
11511154
tm.assert_index_equal(result, expected)
11521155
assert result[0].tz.zone == "US/Central"

0 commit comments

Comments
 (0)