ENH: Intersection, Union and Difference methods for Interval and IntervalArray
State: closed

- [x] closes #21998
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [x] Added an entry in the latest `doc/source/whatsnew/v3.0.0.rst` file if fixing a bug or adding a new feature.

### 1) Description
Me and @Leventide have implemented the following methods for Interval and IntervalArray operations:

**1.1) Interval methods**
**-  Intersection**
The 'Intersection' method returns the common points between two intervals in a form of Interval or None if there is no intersection.

**- Union**
The 'Union' method returns all the points present in atleast one of the intervals. It returns this result in a form of an numpy array of Intervals.

**- Difference**
The 'Difference' method returns the points in the first interval that aren't contained in the second interval. It returns this result in a form of an numpy array of Intervals.

**1.2) IntervalArray methods**
The corresponding methods for the IntervalArray apply the operation element by element. In other words, it calculates the result based on each interval in the array with the given interval.

**-  Intersection**
The 'Intersection' method returns an numpy array of Intervals containing the Intersections between each interval and the given one.

**- Union**
The 'Union' method returns an array of arrays of Intervals containing the unions between each interval and the given one.

**- Difference**
The 'Difference' method returns an array of arrays of Intervals containing the differences between each interval and the given one.

### 2) Why didn't we use IntervalArray instead of numpy arrays:
IntervalArray can only contain Intervals that are equally closed. For operations such as union and difference between intervals, since the result can have two intervals that aren't equally closed, using IntervalArray wouldn't return the proper result.

### 3) Are there other implementations of these methods already available?
We saw that https://piso.readthedocs.io/en/latest/reference/interval.html has a implementation for these methods. However, since it uses IntervalArray it can only handle correctly operations between intervals that are equally closed. By using numpy arrays we can apply these methods correctly for any kind of interval.



Labels:
name: Stale

Comments:
Risk of confusion with intervalindex set methods?
Possibly. But as far as i can see the only one which might generate confusion is the intersection method. Should we give it another name?
This pull request is stale because it has been open for thirty days with no activity. Please [update](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#updating-your-pull-request) and respond to this comment if you're still interested in working on this.
This pull request is stale because it has been open for thirty days with no activity. Please [update](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#updating-your-pull-request) and respond to this comment if you're still interested in working on this.
Thanks for the PR, but it appears the core team isn't fully enthusiastic to support this feature at the moment, so closing. Happy to reopen if there's renewed interest.
I'm very interested in this feature, as well as a smarter `intersection` method for `IntervalIndex` that would build on top of this PR. Would love to know more why the core team is not enthusiastic about this feature.