Change IntervalIndex set-ops error code type by topper-123 · Pull Request #19329 · pandas-dev/pandas (original) (raw)

Set operations (union, difference...) on IntervalIndex with incompatible index types will now raise a TypeError rather than a ValueError.

This PR is needed to make the changes requested in #19021.

EDIT: I've improved the error message also. Previously, you'd get:

pd.IntervalIndex.from_breaks([0,1,2,3]).union(pd.RangeIndex(3)) ValueError: can only do set operations between two IntervalIndex objects that are closed on the same side

Which made no sense in this case. Now we get:

pd.IntervalIndex.from_breaks([0,1,2,3]).union(pd.RangeIndex(3)) TypeError: the other index needs to be an IntervalIndex too, but was type RangeIndex