Skip to content

ENH: Interval.__contains__(self, other: Interval) #46613

Closed
@choucavalier

Description

@choucavalier

Is your feature request related to a problem?

Not a problem. Just a feature I would have liked to have! :-)

I would like to be able to know if an Interval is contained into another Interval.

e.g. [2, 3) is contained in [2, 4) but not in (2, 4]

Describe the solution you'd like

Something like this

def interval_contains(a: pd.Interval, b: pd.Interval) -> bool:
    res = a.left <= b.left and a.right >= b.right
    if a.open_left and b.closed_left:
        res &= a.left < b.left
    if a.open_right and b.closed_right:
        res &= a.right > b.right
    return res

API breaking implications

Currently, if you do something like this

a = pd.Interval(2, 3, closed="left")
b = pd.Interval(2, 4, closed="left")
print(a in b)

it raises the following exception

TypeError: __contains__ not defined for two intervals

Adding support for another type of object in __contains__ would not break the API, but just extend it.

Describe alternatives you've considered

Could not find any.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions