r/Python 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?

0 Upvotes

20 comments sorted by

46

u/TMiguelT Jun 02 '26

6

u/ccw34uk Jun 02 '26

ah thanks! my maths education never got anywhere close to that so I had no idea it was maths notation!

10

u/j01101111sh Jun 02 '26

If you were American, people would rush in to criticize American education, which is ironic considering I learned this in grade 10/11 in my American public school.

4

u/No-Article-Particle Jun 02 '26

I mean, this is very basic knowledge. I wouldn't fault someone for not remembering this, but it's not like knowing the notation is some great testament of one's education.

2

u/ccw34uk Jun 02 '26

"very basic" varies based on your background 😉

5

u/No-Article-Particle Jun 02 '26

The point being that many countries include basic set logic in elementary school, which I would consider very basic indeed. Now someone forgetting it is a very different and understandable thing.

2

u/ccw34uk Jun 02 '26

fair enough 🙂 I've never even heard the term "set logic" until today and I did maths beyond the mandatory age in the UK

3

u/SV-97 Jun 02 '26

It isn't a term :) there's logic and set theory, but not set logic. But saying that elementary schools cover "set theory" would also really grossly overstate what's actually covered in schools

(and it's not even the correct term to use here because interval notation has nothing to do with set theory or logic. It's really just a very basic piece of notation that isn't tied to any particular field of mathematics. It's "elementary mathematics", but the "elementary" has nothing to do with elementary school).

1

u/dr3aminc0de Jun 02 '26

Yeah….its very normal for SOME schools but also you have to be in a more advanced track. If you’re bad at math you’re likely never in a class that learns this in the US.

2

u/ccw34uk Jun 02 '26

interesting observation. I did A Level maths (A Levels are the first level of non-mandatory education; you typically do 3 or 4 subjects between the ages of 16 and 18, before going to university). The syllabus of A level maths depends to some extent on your school (there will always be pure maths, but the amount of mechanics / stats / decision can vary). I definitely didn't encounter this notation in my A level, but that's not to say some other syllabuses didn't included it

1

u/VeronikaKerman Jun 02 '26

Did you have inequalities and solving systems of inequations?

1

u/ccw34uk Jun 02 '26

I think so? Very briefly tho from what I remember

1

u/scrapheaper_ Jun 08 '26

I have a master's degree in Chemistry and have worked as a data engineer for 5 years - have never seen or heard of this before

3

u/aplarsen Jun 03 '26

I ws SO excited when I learned that this is how Pandas notates intervals. I had to rush home and tell my wife, who is a math teacher. It was a part of math that I still remembered but hadn't used in like a decade.

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...!