Skip to content

Commit 122ae44

Browse files
authored
BUG: IntervalIndex.union with mismatched dtypes both empty (#38282)
1 parent 5399c6d commit 122ae44

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ Other
833833
- Bug in :meth:`Index.difference` failing to set the correct name on the returned :class:`Index` in some corner cases (:issue:`38268`)
834834
- Bug in :meth:`Index.union` behaving differently depending on whether operand is an :class:`Index` or other list-like (:issue:`36384`)
835835
- Bug in :meth:`Index.intersection` with non-matching numeric dtypes casting to ``object`` dtype instead of minimal common dtype (:issue:`38122`)
836+
- Bug in :meth:`IntervalIndex.intersection` returning an incorrectly-typed :class:`Index` when empty (:issue:`38282`)
836837
- Passing an array with 2 or more dimensions to the :class:`Series` constructor now raises the more specific ``ValueError`` rather than a bare ``Exception`` (:issue:`35744`)
837838
- Bug in ``dir`` where ``dir(obj)`` wouldn't show attributes defined on the instance for pandas objects (:issue:`37173`)
838839
- Bug in :meth:`Index.drop` raising ``InvalidIndexError`` when index has duplicates (:issue:`38051`)

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2694,7 +2694,7 @@ def union(self, other, sort=None):
26942694
"""
26952695
self._validate_sort_keyword(sort)
26962696
self._assert_can_do_setop(other)
2697-
other = ensure_index(other)
2697+
other, result_name = self._convert_can_do_setop(other)
26982698

26992699
if not self._can_union_without_object_cast(other):
27002700
return self._union_incompatible_dtypes(other, sort=sort)

pandas/core/indexes/interval.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,10 @@ def _intersection_non_unique(self, other: "IntervalIndex") -> "IntervalIndex":
10601060

10611061
def _setop(op_name: str, sort=None):
10621062
def func(self, other, sort=sort):
1063+
# At this point we are assured
1064+
# isinstance(other, IntervalIndex)
1065+
# other.closed == self.closed
1066+
10631067
result = getattr(self._multiindex, op_name)(other._multiindex, sort=sort)
10641068
result_name = get_op_result_name(self, other)
10651069

@@ -1074,7 +1078,7 @@ def func(self, other, sort=sort):
10741078
func.__name__ = op_name
10751079
return setop_check(func)
10761080

1077-
union = _setop("union")
1081+
_union = _setop("union")
10781082
difference = _setop("difference")
10791083
symmetric_difference = _setop("symmetric_difference")
10801084

pandas/core/indexes/multi.py

+5
Original file line numberDiff line numberDiff line change
@@ -3569,6 +3569,11 @@ def union(self, other, sort=None):
35693569
if len(other) == 0 or self.equals(other):
35703570
return self.rename(result_names)
35713571

3572+
return self._union(other, sort=sort)
3573+
3574+
def _union(self, other, sort):
3575+
other, result_names = self._convert_can_do_setop(other)
3576+
35723577
# TODO: Index.union returns other when `len(self)` is 0.
35733578

35743579
if not is_object_dtype(other.dtype):

pandas/tests/indexes/interval/test_setops.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ def test_union(self, closed, sort):
3232
tm.assert_index_equal(index.union(index, sort=sort), index)
3333
tm.assert_index_equal(index.union(index[:1], sort=sort), index)
3434

35+
def test_union_empty_result(self, closed, sort):
3536
# GH 19101: empty result, same dtype
3637
index = empty_index(dtype="int64", closed=closed)
3738
result = index.union(index, sort=sort)
3839
tm.assert_index_equal(result, index)
3940

40-
# GH 19101: empty result, different dtypes
41+
# GH 19101: empty result, different dtypes -> common dtype is object
4142
other = empty_index(dtype="float64", closed=closed)
4243
result = index.union(other, sort=sort)
43-
tm.assert_index_equal(result, index)
44+
expected = Index([], dtype=object)
45+
tm.assert_index_equal(result, expected)
4446

4547
def test_intersection(self, closed, sort):
4648
index = monotonic_index(0, 11, closed=closed)

0 commit comments

Comments
 (0)