ENH: Interval type should support intersection, union & overlaps & difference · Issue #21998 · pandas-dev/pandas (original) (raw)

Problem description

We have the Interval type in pandas, which is extremely useful, however the standard interval arithmetic operations are missing from the pandas implementation. I would be happy to work on this enhancement.

One should be able to do the following with pandas.Interval

The example uses numeric intervals, but the same operations are also valid for time series intervals.

following proposed operations and suggested behaviour:

import pandas as pd i0 = pd.Interval(0, 3, closed='right') i1 = pd.Interval(2, 4, closed='right') i2 = pd.Interval(5, 8, closed='right')

1. intersection

i0.intersection(i1)

should return: pd.Interval(2, 3, closed='right')

i0.intersection(i3)

should return: np.nan (or, perhaps a more appropriate null-interval representation)

2. union

i0.union(i1)

should return: pd.IntervalIndex([pd.Interval(0, 4, closed='right')])

i0.union(i2)

should return: pd.IntervalIndex([pd.Interval(0, 2, closed='right'), pd.Interval(5,8, closed='right')])

  1. overlaps i0.overlaps(i1)

should return: True

i0.overlaps(i2)

should return: False

  1. difference i0.difference(i1)

should return: pd.Interval(0, 2, closed='right')

i0.difference(i2)

should return: pd.interval(0, 3, closed='right')