Research

  • [X] I have searched the [pandas] tag on StackOverflow for similar questions.

  • [X] I have asked my usage related question on StackOverflow.

Link to question on StackOverflow

https://stackoverflow.com/questions/55832639/dataframe-filter-a-column-if-it-is-in-an-interval-data-type

Question about pandas

Top voted SO answer gives:

data.loc[(data.A>=interval.left) & (data.A<interval.right)]

But this doesn't account for interval.closed. Obviously that snippet could be expanded but check, but I feel like this should be supported out of the box?


IntervalIndex feels like it's close, but this restriction makes it a no-go for my case:

intervals that are closed on the same side.


Related: https://github.com/pandas-dev/pandas/issues/21998

Comment From: jbrockmendel

df = pd.DataFrame({'A': range(1, 10)})
obj = pd.Interval(1,4)

ser = pd.Series(obj, index=df.index)
arr = ser.array

>>> arr.contains(df["A"])
0    False
1     True
2     True
3     True
4    False
5    False
6    False
7    False
8    False
Name: A, dtype: bool

Comment From: phofl

Closing, please ping if this does not answer your question

Comment From: davetapley

Thanks @jbrockmendel, @phofl, that works great, but the typing is broken in VSCode, I get:

Cannot access member "contains" for type "ExtensionArray"
  Member "contains" is unknownPylance[reportGeneralTypeIssues](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportGeneralTypeIssues)
(function) contains: Unknown

Worth a separate bug report?

Comment From: phofl

This should go into the pandas-stubs repo