Skip to content

DEPR: DatetimeIndex.union with mixed timezones #41458

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

Merged
merged 2 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

.. ---------------------------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/indexes/datetimes/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down