Sub topic 4
Which side is closed: right and include_lowest
This chapter assumes you can create a pandas Series, pass a list of bin edges to pd.cut, and read a basic Series printout. Here you will learn how pandas decides which bin a value on an edge belongs to.
Edges belong to one bin only
Bin edges are shared by two neighbouring bins. With edges [0, 10, 20], the value 10 could go in the first bin or the second. pandas follows a fixed rule. Each bin is a half-open interval, closed on one side and open on the other.
By default right=True, so each interval includes its right edge and excludes its left edge. This is written (0, 10]. Parentheses mean the edge is excluded. Square brackets mean it is included.
Seeing the effect
This example puts a value on every edge:
import pandas as pd
values = pd.Series([0, 10, 15, 20])
print(pd.cut(values, bins=[0, 10, 20]))
print(pd.cut(values, bins=[0, 10, 20], include_lowest=True))
print(pd.cut(values, bins=[0, 10, 20], right=False))Here is where each value lands:
| Call | Intervals | 0 | 10 | 15 | 20 |
|---|---|---|---|---|---|
| default | (0, 10], (10, 20] | NaN | (0, 10] | (10, 20] | (10, 20] |
| `include_lowest=True` | first bin widened to include 0 | first bin | (0, 10] | (10, 20] | (10, 20] |
| `right=False` | [0, 10), [10, 20) | [0, 10) | [10, 20) | [10, 20) | NaN |
The diagram shows how the default rule treats the value 10.
The two parameters
right chooses which side is closed.
right=True(default) gives(a, b]. The right edge is included.right=Falsegives[a, b). The left edge is included.
include_lowest fixes a common problem with the default. The smallest edge, here 0, is excluded from the first bin, so a value of 0 becomes NaN. Setting include_lowest=True makes the first interval include its left edge. pandas shows this interval with a slightly lowered edge, such as (-0.001, 10.0]. That is a display adjustment, and your data is unchanged.
Choosing a setting
Pick the closure that matches how the categories are defined in your data.
- Ages "up to and including 18" suggest
right=True. - Brackets such as "18 to under 65" suggest
right=False. - If your data contains the minimum edge value, such as 0, use
include_lowest=Truewith the default closure.
A reliable habit is to check the minimum and maximum of your data against the first and last edges before binning. A single isna().sum() on the result also catches mistakes quickly.
import pandas as pd
values = pd.Series([0, 10, 15, 20])
binned = pd.cut(values, bins=[0, 10, 20])
print(binned.isna().sum()) # 1, because 0 is not in (0, 10]Common errors
- Choosing edges that start at the data minimum, then losing that row to NaN.
- Mixing up
[and(when reading the output. The bracket marks the closed side. - Expecting
include_lowest=Trueto include the top edge underright=False. It does not.
Exercise
You run pd.cut(pd.Series([5, 10, 20]), bins=[5, 10, 20]) and get NaN for the value 5. Give two different ways to fix it, and say which interval 10 falls into under each.
Show answer
Use include_lowest=True, which widens the first bin so that 5 is included. The value 10 stays in the first bin, shown as (4.995, 10.0]. Alternatively use right=False, giving [5, 10) and [10, 20). Now 5 is in the first bin and 10 moves to the second bin, but 20 becomes NaN because it is outside [10, 20).