0

I know you typically select parts of a pd.DataFrame with a boolean mask or via the : operator. I was wondering whether it is possible to do indexing into a pd.DataFrame using a pd.Interval, i.e. I want to accomplish:

import pandas as pd

df = pd.DataFrame({'A': ['a','b','c','d','e']})  # df has simple RangeIndex(start=0, stop=5, step=1)
interval = pd.Interval(left=1, right=3, closed='both')

print(df[interval])  # I want items 'b','c','d', but get KeyError

So instead I have to go with the more verbose

print(df[interval.left: interval.right])

Indexing via a defined start- and end-point seems like a feature that should be existing, but can't find in the docs. Am I missing something?

sh0rtcircuit
  • 445
  • 3
  • 13
  • Is there a reason you specifically want `pd.Interval` for this behavior? In your existing example, you could just do something like `df.loc[range(1, 4)]` – Randy Feb 05 '20 at 20:35
  • In a real-world use case, I'm thinking more of accessing a time series with `DatetimeIndex` and `pd.Interval` with `pd.Timestamp`, in which case the `range` approach would not work as simply. – sh0rtcircuit Feb 05 '20 at 20:43

0 Answers0