Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
choucavalier opened this issue Apr 2, 2022 · 7 comments · Fixed by #47927
Closed

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

choucavalier opened this issue Apr 2, 2022 · 7 comments · Fixed by #47927
Assignees
Labels
Enhancement Interval Interval data type

Comments

@choucavalier
Copy link
Contributor

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.

@choucavalier choucavalier added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 2, 2022
@choucavalier
Copy link
Contributor Author

@jreback thanks for your reply. I know about what you just referred to. It works with scalar values but it does not work with another Interval. Therefore, I am not sure how your answer is relevant to the original feature request.

@samukweku
Copy link
Contributor

@tgy is this similar to interval overlap? Maybe interval overlaps could apply

@choucavalier
Copy link
Contributor Author

@tgy is this similar to interval overlap? Maybe interval overlaps could apply

Thanks for your reply. I know about the overlaps() method of Interval, as I read the documentation and the code before opening this issue.

Interval containment is different from interval overlap.

First of all, the overlaps operator is commutative. In other words, for any given two intervals a and b, we have that a.overlaps(b) iff b.overlaps(a). This is not the case with the containment operator: if a is contained in b, then b is not necessarily contained in a (the latter is true iff b = a).

This simple observation is already enough to understand that these two operators are different. But let's take a concrete example as well.

The two intervals a = [2, 4) and b = (2, 5] overlap but neither a contains b nor b contains a.

@venaturum
Copy link
Contributor

You can find this functionality in piso until it is implemented in pandas

https://piso.readthedocs.io/en/latest/reference/api/piso.interval.issuperset.html#piso.interval.issuperset

@choucavalier
Copy link
Contributor Author

ah thanks @venaturum! that package fell under my radar. interesting.

@mroeschke mroeschke added Interval Interval data type and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 6, 2022
@kapiliyer
Copy link
Contributor

take

mroeschke pushed a commit that referenced this issue Aug 15, 2022
* ENH: Support For Interval __contains__ Other Interval (#46613)

* ENH: Support For Interval __contains__ Other Interval (#46613)

* Update doc/source/whatsnew/v1.5.0.rst

Co-authored-by: Valentin Iovene <[email protected]>

* ENH: Support For Interval __contains__ Other Interval (#46613)

* ENH: Support For Interval __contains__ Other Interval (#46613)

* Fix: Unintentionally Modified Range

* ENH: Support For Interval __contains__ Other Interval (#46613)

* Fix: Unintentionally Modified Range

* ENH: Support For Interval __contains__ Other Interval (#46613)

Co-authored-by: Valentin Iovene <[email protected]>
YYYasin19 pushed a commit to YYYasin19/pandas that referenced this issue Aug 23, 2022
…) (pandas-dev#47927)

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

* Update doc/source/whatsnew/v1.5.0.rst

Co-authored-by: Valentin Iovene <[email protected]>

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

* Fix: Unintentionally Modified Range

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

* Fix: Unintentionally Modified Range

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

Co-authored-by: Valentin Iovene <[email protected]>
noatamir pushed a commit to noatamir/pandas that referenced this issue Nov 9, 2022
…) (pandas-dev#47927)

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

* Update doc/source/whatsnew/v1.5.0.rst

Co-authored-by: Valentin Iovene <[email protected]>

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

* Fix: Unintentionally Modified Range

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

* Fix: Unintentionally Modified Range

* ENH: Support For Interval __contains__ Other Interval (pandas-dev#46613)

Co-authored-by: Valentin Iovene <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Interval Interval data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants