Sub topic 11
Inspecting the computed edges with retbins
This chapter assumes you can call pd.cut and pd.qcut, and that you know how right-closed intervals work. It shows how to get the bin edges back from either function.
Why you need the edges
When you pass explicit edges to pd.cut, you already know them. In two cases you don't:
- You give
pd.cutan integer such asbins=3, and pandas computes the edges. - You call
pd.qcut, and the edges come from the data's quantiles.
In both cases the result only shows each row's interval. The retbins=True parameter makes the function return a tuple of (binned, edges).
Edges from an integer bin count
import pandas as pd
values = pd.Series([1, 4, 7, 10])
binned, edges = pd.cut(values, bins=3, retbins=True)
print(edges)The three equal-width bins span 1 to 10, so the interior edges are 4 and 7. The first edge is not exactly 1. For right-closed intervals, pandas moves the lowest edge down by 0.1% of the range, to about 0.991. This keeps the minimum value inside the first bin without needing include_lowest.
Edges from quantiles
pd.qcut computes its edges from the data, so retbins=True is the easiest way to see them. This example reuses the repeated-values case from the previous chapter.
import pandas as pd
values = pd.Series([1, 1, 1, 1, 1, 2, 3, 4])
binned, edges = pd.qcut(values, q=4, duplicates="drop", retbins=True)
print(edges)
print(binned.cat.categories)The quantile edges are 1, 1, 1, 2.25 and 4. After duplicates="drop", edges is [1.0, 2.25, 4.0]. That gives two bins, not four. Comparing len(edges) - 1 with your requested q is a quick check that bins were lost.
The table below shows what edges contains for each kind of bins argument.
| `bins` argument | Returned edges |
|---|---|
| Integer, as in `pd.cut(x, 3)` | Array of computed equal-width edges |
| List of edges | Array holding your edges |
| Integer or fractions, as in `pd.qcut` | Array of quantile values from the data |
| `IntervalIndex` | The same `IntervalIndex` |
Reusing edges on new data
The main use of retbins is applying the same cut points to a second dataset, such as new data scored against training thresholds. Running qcut on the new data would produce different edges.
import pandas as pd
train = pd.Series([1, 1, 1, 1, 1, 2, 3, 4])
new = pd.Series([1, 2, 3, 10])
_, edges = pd.qcut(train, q=4, duplicates="drop", retbins=True)
print(pd.cut(new, bins=edges, include_lowest=True))The first edge from qcut equals the training minimum. Without include_lowest=True, the value 1 would fall outside (1.0, 2.25] and become NaN. The value 10 is above the last edge, so it is NaN either way. Check for such rows with isna() before you use the result.
Common mistakes
- Forgetting that the return value is a tuple.
- Expecting
len(edges)to equalq + 1afterduplicates="drop". - Reusing quantile edges without
include_lowest=True, which turns the training minimum intoNaN. - Expecting new values outside the training range to be binned. They become
NaN.
Exercise
You run binned, edges = pd.qcut(s, q=5, duplicates="drop", retbins=True) and edges has 4 elements. How many bins does binned have, and which argument would you add to pd.cut to make the lowest edge value usable when you apply edges to new data?
Show answer
Four elements means three bins (len(edges) - 1), so two of the five requested bins were lost to duplicate edges. When applying edges with pd.cut, add include_lowest=True so the value equal to the first edge is placed in the first bin instead of becoming NaN.