Skip to content

Features which Interval / IntervalIndex should probably have #19480

Open
@alexlenail

Description

@alexlenail

Here's a list of possible features which should be added to the Interval and IntervalIndex types. Some of them might already exist, in which case, please excuse my mistake. Also note, I'm not asking for these, just listing some ideas I had which might be useful for others.

closest

>>> Interval(3, 4).closest(IntervalIndex.from_tuples([(1, 2), (5, 9)]), how="min|mean|max")
>>> IntervalIndex.from_tuples([(1, 2), (5, 9)]).closest(IntervalIndex.from_tuples([(1, 4), (2, 5)]))

Big question: what if there are two or more identically distant intervals?

complement

>>> IntervalIndex.from_tuples([(1, 2), (5, 9)]).complement(lower_bound=np.neginf, upper_bound=np.inf)
IntervalIndex.from_tuples([(np.neginf, 1), (2, 5), (9, np.inf)])

intersection

>>> IntervalIndex.from_tuples([(1, 2), (5, 9)]).intersect(IntervalIndex.from_tuples([(3, 4), (6, 7)]))
IntervalIndex.from_tuples([(6, 7)])
>>> IntervalIndex.from_tuples([(1, 2), (5, 9)]).intersect(Interval(6, 7))
???

union

>>> IntervalIndex.from_tuples([(1, 2), (5, 9)]).intersect(IntervalIndex.from_tuples([(3, 4), (6, 7)]))
IntervalIndex.from_tuples([(1, 2), (3, 4), (5, 9)])

subtract|difference

>>> IntervalIndex.from_tuples([(1, 2), (5, 9)]) - IntervalIndex.from_tuples([(3, 4), (6, 7)])
IntervalIndex.from_tuples([(1, 2), (5, 6), (7, 9)])
>>> IntervalIndex.from_tuples([(1, 2), (5, 9)]) - Interval(6, 7)
IntervalIndex.from_tuples([(1, 2), (5, 6), (7, 9)])
>>> Interval(6, 7) - IntervalIndex.from_tuples([(1, 2), (5, 9)])
???

sort

(I'm guessing this is probably already implicitly implemented. Does it work for multi-index in which one of the levels is Intervalindex?)

shift

>>> IntervalIndex.from_tuples([(1, 2), (5, 9)]).shift(2)
IntervalIndex.from_tuples([(3, 4), (7, 11)])

(Could be really useful for datetimes?)

slop|grow|window|better name

>>> IntervalIndex.from_tuples([(1, 2), (5, 9)]).slop(1)
IntervalIndex.from_tuples([(0, 3), (4, 10)])

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions