Search code examples
pythonpandascategoriesparenthesesbrackets

Why are pandas categories/bins written with an opening parenthesis but a closing bracket?


I came across the following on the pandas doc page for pd.cut():

Discretize into three equal-sized bins.

>>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), 3)
... 
[(0.994, 3.0], (5.0, 7.0], (3.0, 5.0], (3.0, 5.0], (5.0, 7.0], ...
Categories (3, interval[float64]): [(0.994, 3.0] < (3.0, 5.0] ...

Why are the returned categories opened with a parenthesis ( but closed by a bracket ]? Does this denominate a special object in Python? At first I thought it was a typo but my console gives the same result.


Solution

  • Because they denote intervals, wherein interval notation dictates that:

    • a square bracket is inclusive;
    • a parentheses is exclusive.

    If a is your result, you'll see that a.categories gives you a Pandas IntervalIndex.