Knowledge base / Python

pandas cut: intervals, boundaries and missing categories

The value 25 can belong to the first interval while 0 is excluded. I show a complete example and explain what happens exactly at the boundaries.

Kajo RudzińskiPublished Updated 6 min read

Three sorting intervals of different widths and a block outside the range, in yellow and navy pixel art.

My most useful question about cut often concerns an apparently harmless value: one exactly equal to a boundary. If a table has intervals ending at 25, 50 and 100, we still need to establish where 25 belongs. Otherwise, the reader and the program may interpret the same label differently.

I write down the rule before showing code. These synthetic examples follow the pandas 3.1.1 documentation available in November 2026. The tables show assignments derived from those rules. The dataset is deliberately small enough to trace every boundary.

Fixed edges and intervals closed on the right

pandas.cut assigns one-dimensional data to intervals. With explicit edges and the default right=True, each interval excludes its left edge and includes its right edge. The notation (0, 25] means greater than 0 and no greater than 25.

import pandas as pd

scores = pd.Series([5, 15, 25, 35, 45, 55, 65, 75, 85, 95])
edges = [0, 25, 50, 100]

groups = pd.cut(
    scores,
    bins=edges,
    labels=["Low", "Medium", "High"],
    right=True,
)

Four edges define three intervals. The labels are arbitrary names rather than actual examination grades. Explain them to the user, especially if “High” will affect which observations receive further analysis.

score interval label
5 (0, 25] Low
15 (0, 25] Low
25 (0, 25] Low
35 (25, 50] Medium
45 (25, 50] Medium
55 (50, 100] High
65 (50, 100] High
75 (50, 100] High
85 (50, 100] High
95 (50, 100] High

The first group contains three observations, the second two and the third five. The value 25 still belongs to Low. A loose phrase such as “from 25” should not overrule the actual inclusion rule. The description must match the endpoints used by the operation.

What right=False changes

With right=False, the same edges produce [0, 25), [25, 50) and [50, 100). The left edge is included and the right edge excluded. The value 25 moves into the middle group, while 100 falls outside the final interval.

left_closed = pd.cut(
    pd.Series([0, 25, 50, 100]),
    bins=edges,
    labels=["Low", "Medium", "High"],
    right=False,
)
value default right=True right=False
0 missing Low
25 Low Medium
50 Medium High
100 High missing

The default variant in this table does not use include_lowest. That argument makes a separate choice about the first lower edge possible. I would not switch to right=False merely to include zero without checking what happens at the top of the range.

Include the first lower edge

To keep right-closed intervals while admitting zero, use include_lowest=True:

including_zero = pd.cut(
    pd.Series([0, 25, 50, 100]),
    bins=edges,
    labels=["Low", "Medium", "High"],
    right=True,
    include_lowest=True,
)

Now 0 and 25 receive Low, 50 receives Medium and 100 receives High. Named labels avoid the need to interpret a small adjustment to the displayed lower interval boundary. The business definition should still say explicitly that zero is admitted.

The same detail matters for grading. With edges 0, 50, 70, 90 and 100, right=True places 90 in (70, 90]. If that interval is named “Good”, 90 receives Good. The highest group starts above 90, rather than including 90.

A block beside a post separating neighbouring interval channels, in yellow and navy pixel art.

Supply a bin count instead of explicit edges

Passing bins=4 splits the data range into four equal-width parts. It does not guarantee equal counts. Our main dataset ranges from 5 to 95, making the basic width 22.5.

automatic, automatic_edges = pd.cut(
    scores,
    bins=4,
    retbins=True,
)

With the default right closure, the bottom edge is extended slightly to include the minimum. For this sample, the edges are 4.91, 27.5, 50, 72.5 and 95. The distribution is:

interval observations
(4.91, 27.5] 3
(27.5, 50] 2
(50, 72.5] 2
(72.5, 95] 3

The maximum remains 95. I would not change the upper boundary to 100 merely because it looks round. The explicit edges list from the first example and the automatic split express different rules. For a report comparing periods, I would choose fixed agreed edges when categories must retain their meaning across months.

When qcut is appropriate

qcut uses quantiles, aiming for groups with similar numbers of observations. Their widths can differ. With many repeated values, boundaries can coincide, requiring a decision about an error or dropping duplicate edges.

“Automatic binning” therefore should not be described as an unconditional promise of equal groups. First ask whether you care about a fixed threshold, interval width or position within the distribution. Those are different goals. The entry about pandas qcut develops the quantile-based alternative.

Investigate missing category assignments

With explicit edges, out-of-range values have no assigned interval. Missing inputs also remain unassigned. The missing-data guide explains detection with isna. I would distinguish an unknown number from a known number rejected by the chosen thresholds.

boundary_scores = pd.Series([0, 25, 100, 105, None])
boundary_groups = pd.cut(
    boundary_scores,
    bins=edges,
    labels=["Low", "Medium", "High"],
    include_lowest=True,
)

The first three assignments are Low, Low and High. The classifications for 105 and the missing input are both missing, but their causes differ. Automatically calling both Low would hide a range problem and a completeness problem.

For a count check, use boundary_groups.value_counts(dropna=False). The value_counts documentation explains that missing values are excluded by default. I want to see them so that the classified subset does not pretend to represent the full input.

Labels, precision and duplicate edges

The number of labels must match the number of resulting intervals. labels=False returns interval indicators rather than names. These are group codes, rather than original scores. I would not average them as though the difference between code 0 and code 1 had the unit of the source data.

The precision argument concerns interval-label representation. It does not round examination scores before classification. If the input needs rounding, make that a separate explicit rule. Otherwise, a value just below a threshold may be interpreted differently from what the user expects.

Duplicate edges raise an error by default. Setting duplicates="drop" can reduce the number of intervals, making it more than a neutral way to silence a problem. Revisit the labels and group definitions afterwards. I would prefer to notice an invalid configuration before publication rather than produce a seemingly complete table with a different segmentation.

Exercise before reporting

For edges 0, 25, 50 and 100, write down the assignments for 0, 24, 25, 26, 100 and 101. Use right-closed intervals with zero included. The expected sequence is Low, Low, Low, Medium, High and missing. Then change the closure and explain what happens to 25 and 100.

Once the rule is clear, summarise groups with pandas crosstab. Keep the edges and missing-value policy in the report description. A label such as “high score” gives too little information to someone making a decision.

Key takeaways

Which interval receives a value equal to a boundary?

With the default right=True, intervals include the right edge, so edges 0, 25, 50, 100 place 25 in (0, 25]. With right=False, 25 belongs to [25, 50).

Does bins=4 create four groups with equal numbers of observations?

No. It creates equal-width intervals with an extension at the outer edge. qcut groups by quantiles, and repeated values may require an additional decision about duplicate boundaries.

Why is the category missing after cut?

The input may be missing, outside the specified range or equal to an excluded endpoint. Check those cases separately. A missing assignment does not automatically mean the lowest category.

AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.