Skip to content

Commit 4003a74

Browse files
topper-123jreback
authored andcommitted
Change InterValindex set-ops eror code type (#19329)
1 parent 55db35e commit 4003a74

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ Other API Changes
312312
- Addition or subtraction of ``NaT`` from :class:`TimedeltaIndex` will return ``TimedeltaIndex`` instead of ``DatetimeIndex`` (:issue:`19124`)
313313
- :func:`DatetimeIndex.shift` and :func:`TimedeltaIndex.shift` will now raise ``NullFrequencyError`` (which subclasses ``ValueError``, which was raised in older versions) when the index object frequency is ``None`` (:issue:`19147`)
314314
- Addition and subtraction of ``NaN`` from a :class:`Series` with ``dtype='timedelta64[ns]'`` will raise a ``TypeError` instead of treating the ``NaN`` as ``NaT`` (:issue:`19274`)
315+
- Set operations (union, difference...) on :class:`IntervalIndex` with incompatible index types will now raise a ``TypeError`` rather than a ``ValueError`` (:issue:`19329`)
315316

316317
.. _whatsnew_0230.deprecations:
317318

pandas/core/indexes/interval.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1152,12 +1152,17 @@ def insert(self, loc, item):
11521152
new_right = self.right.insert(loc, right_insert)
11531153
return self._shallow_copy(new_left, new_right)
11541154

1155-
def _as_like_interval_index(self, other, error_msg):
1155+
def _as_like_interval_index(self, other):
11561156
self._assert_can_do_setop(other)
11571157
other = _ensure_index(other)
1158-
if (not isinstance(other, IntervalIndex) or
1159-
self.closed != other.closed):
1160-
raise ValueError(error_msg)
1158+
if not isinstance(other, IntervalIndex):
1159+
msg = ('the other index needs to be an IntervalIndex too, but '
1160+
'was type {}').format(other.__class__.__name__)
1161+
raise TypeError(msg)
1162+
elif self.closed != other.closed:
1163+
msg = ('can only do set operations between two IntervalIndex '
1164+
'objects that are closed on the same side')
1165+
raise ValueError(msg)
11611166
return other
11621167

11631168
def _concat_same_dtype(self, to_concat, name):
@@ -1296,9 +1301,7 @@ def equals(self, other):
12961301

12971302
def _setop(op_name):
12981303
def func(self, other):
1299-
msg = ('can only do set operations between two IntervalIndex '
1300-
'objects that are closed on the same side')
1301-
other = self._as_like_interval_index(other, msg)
1304+
other = self._as_like_interval_index(other)
13021305

13031306
# GH 19016: ensure set op will not return a prohibited dtype
13041307
subtypes = [self.dtype.subtype, other.dtype.subtype]

pandas/tests/indexes/interval/test_interval.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -934,12 +934,14 @@ def test_set_operation_errors(self, closed, op_name):
934934
set_op = getattr(index, op_name)
935935

936936
# non-IntervalIndex
937-
msg = ('can only do set operations between two IntervalIndex objects '
938-
'that are closed on the same side')
939-
with tm.assert_raises_regex(ValueError, msg):
937+
msg = ('the other index needs to be an IntervalIndex too, but '
938+
'was type Int64Index')
939+
with tm.assert_raises_regex(TypeError, msg):
940940
set_op(Index([1, 2, 3]))
941941

942942
# mixed closed
943+
msg = ('can only do set operations between two IntervalIndex objects '
944+
'that are closed on the same side')
943945
for other_closed in {'right', 'left', 'both', 'neither'} - {closed}:
944946
other = self.create_index(closed=other_closed)
945947
with tm.assert_raises_regex(ValueError, msg):

0 commit comments

Comments
 (0)