I am trying to generate an IntervalIndex with intervals closed on the right-side, but with the first interval also closed on the left-side. Something like: [[0, 0.4], (0.4, 0.6], (0.6, 1]].
But I am not able to create such an IntervalIndex: the various methods to construct an IntervalIndex accept a closed option that specifies how all intervals should be closed, but it is not possible to specify a different closure for the extreme intervals:
breaks = [0, 0.4, 0.6, 1]
In: pd.IntervalIndex.from_breaks(breaks, closed="right")
Out: IntervalIndex([(0.0, 0.4], (0.4, 0.6], (0.6, 1.0]],
closed='right',
dtype='interval[float64]')
I think this should be possible since pd.cut function has an include_lowest option that does exactly that (I assume that pd.cut creates an IntervalIndex under the hood, am I right?). But I cannot use pd.cut directly with the breaks list: I need an IntervalIndex object. I need to preserve the exact IntervalIndex that is used by pd.cut.
I have tried creating an IntervalIndex directly from Interval objects:
pd.IntervalIndex([pd.Interval(0, 0.4, closed="both"),
pd.Interval(0.4, 0.6, closed="right"),
pd.Interval(0.6, 1, closed="right")
])
but I get ValueError: intervals must all be closed on the same side.
I was able to find a workaround for my problem: using -0.0001 instead of 0 in my breaks list, so that, even though the first interval is open on the left, it actually includes 0. But this is a very hacky solution, in my opinion! Isn't there a better way?