Skip to content

Commit e46f0f1

Browse files
committed
always check subtypes
1 parent 3e3f3e9 commit e46f0f1

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

pandas/core/indexes/interval.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -1285,40 +1285,37 @@ def equals(self, other):
12851285
self.right.equals(other.right) and
12861286
self.closed == other.closed)
12871287

1288-
def _setop(op_name, check_subtypes=False):
1288+
def _setop(op_name):
12891289
def func(self, other):
12901290
msg = ('can only do set operations between two IntervalIndex '
12911291
'objects that are closed on the same side')
12921292
other = self._as_like_interval_index(other, msg)
12931293

1294-
if check_subtypes:
1295-
# GH 19016: ensure set op will not return a prohibited dtype
1296-
subtypes = [self.dtype.subtype, other.dtype.subtype]
1297-
result_subtype = find_common_type(subtypes)
1298-
if is_object_dtype(result_subtype):
1299-
msg = ('can only do {op} between two IntervalIndex '
1300-
'objects that have compatible dtypes')
1301-
raise TypeError(msg.format(op=op_name))
1302-
else:
1303-
result_subtype = self.dtype.subtype
1294+
# GH 19016: ensure set op will not return a prohibited dtype
1295+
subtypes = [self.dtype.subtype, other.dtype.subtype]
1296+
common_subtype = find_common_type(subtypes)
1297+
if is_object_dtype(common_subtype):
1298+
msg = ('can only do {op} between two IntervalIndex '
1299+
'objects that have compatible dtypes')
1300+
raise TypeError(msg.format(op=op_name))
13041301

13051302
result = getattr(self._multiindex, op_name)(other._multiindex)
13061303
result_name = self.name if self.name == other.name else None
13071304

13081305
# GH 19101: ensure empty results have correct dtype
13091306
if result.empty:
1310-
result = result.values.astype(result_subtype)
1307+
result = result.values.astype(self.dtype.subtype)
13111308
else:
13121309
result = result.values
13131310

13141311
return type(self).from_tuples(result, closed=self.closed,
13151312
name=result_name)
13161313
return func
13171314

1318-
union = _setop('union', check_subtypes=True)
1315+
union = _setop('union')
13191316
intersection = _setop('intersection')
13201317
difference = _setop('difference')
1321-
symmetric_difference = _setop('symmetric_difference', check_subtypes=True)
1318+
symmetric_difference = _setop('symmetric_difference')
13221319

13231320
# TODO: arithmetic operations
13241321

pandas/tests/indexes/interval/test_interval.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ def test_union(self, closed):
888888
# GH 19101: empty result, different dtypes
889889
other = IntervalIndex(np.array([], dtype='float64'), closed=closed)
890890
result = index.union(other)
891-
tm.assert_index_equal(result, other)
891+
tm.assert_index_equal(result, index)
892892

893893
def test_intersection(self, closed):
894894
index = self.create_index(closed=closed)
@@ -945,7 +945,6 @@ def test_symmetric_difference(self, closed):
945945
other = IntervalIndex.from_arrays(index.left.astype('float64'),
946946
index.right, closed=closed)
947947
result = index.symmetric_difference(other)
948-
expected = IntervalIndex(np.array([], dtype='float64'), closed=closed)
949948
tm.assert_index_equal(result, expected)
950949

951950
@pytest.mark.parametrize('op_name', [
@@ -968,13 +967,9 @@ def test_set_operation_errors(self, closed, op_name):
968967

969968
# GH 19016: incompatible dtypes
970969
other = interval_range(Timestamp('20180101'), periods=9, closed=closed)
971-
if op_name in ('union', 'symmetric_difference'):
972-
msg = ('can only do {op} between two IntervalIndex objects '
973-
'that have compatible dtypes').format(op=op_name)
974-
with tm.assert_raises_regex(TypeError, msg):
975-
set_op(other)
976-
else:
977-
# should not raise
970+
msg = ('can only do {op} between two IntervalIndex objects that have '
971+
'compatible dtypes').format(op=op_name)
972+
with tm.assert_raises_regex(TypeError, msg):
978973
set_op(other)
979974

980975
def test_isin(self, closed):

0 commit comments

Comments
 (0)