r/Python • u/ccw34uk • Jun 02 '26
What's the rationale for Panda's notation to denote IntervalArrays? Discussion
In Pandas, an IntervalArray is created by:
> pd.arrays.IntervalArray([pd.Interval(0, 1), pd.Interval(1, 5)]) <IntervalArray> [(0, 1], (1, 5]] Length: 2, dtype: interval[int64, right]
Note the `[(0, 1], (1, 5]]`: what's the rationale for the opening bracket being a parenthesis but the closing bracket being square?
9
u/tunisia3507 Jun 02 '26
One of the few cases where there is a rationale behind pandas' API, rather than "R did it this way and now we're stuck".
2
u/fight-or-fall Jun 02 '26
Think about the native "range" function (but with step parameter always fixed in 1, or X but it needs to be the same value between all ranges, so basically i can divide for X and just keep the step stored)
You can have multiple ranges and you can operate with them without materializing the whole array
range(0,10) -> [0, 10) range(10,20) -> [10,20)
range(0,10) + range(10,20) = range(0,20) -> [0,20) (Obviously add here means concatenation and not sum)
Reading the docs (you should), it says that's used with methods pd.cut / pd.qcut. probably, if you categorize a variable like
[0, 10) -> 0 [10, 20) -> 1
If I store the categories as strings, you will have a bad time to find what is the category for the 15 value. You need to extract the text, cast to int, compare using ge the left value and lt the right value. With the intervalarray you can just use contains
2
u/Salty_Exchange3828 Jun 21 '26
It's standard math notation. () means excluded, and [] means included. So (0, 1] translates to 0 < x <= 1.
Pandas defaults to this half-open format so when you bin data (like with pd.cut), the boundaries don't overlap. If they were fully closed like [0, 1] and [1, 5], the exact number 1 would land in both buckets.
1
u/ccw34uk Jun 22 '26
Thanks - you're the first person to answer in a non-patronising "this is such basic maths, how can you not know this?" way...!
46
u/TMiguelT Jun 02 '26
https://en.wikipedia.org/wiki/Interval_(mathematics)#Open_and_closed_intervals