Skip to content

Commit 181cb25

Browse files
committed
fix tablereport error in histogram when data range is very narrow
1 parent c072a97 commit 181cb25

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

skrub/_reporting/_plotting.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ def _get_range(values, frac=0.2, factor=3.0):
193193
return low, high
194194

195195

196+
def _get_safe_hist_range(values):
197+
# make sure numpy can find bin edges between the range low and high bounds
198+
vmin, vmax = values.min(), values.max()
199+
delta = max(np.spacing(vmin), np.spacing(vmax))
200+
if vmax - vmin > 12 * delta:
201+
return None
202+
return vmin - 6 * delta, vmax + 6 * delta
203+
204+
196205
def _robust_hist(col, ax=None, color=None):
197206
col = sbd.drop_nulls(col)
198207
if sbd.is_float(col):
@@ -218,12 +227,16 @@ def _robust_hist(col, ax=None, color=None):
218227
n_low_outliers = (values < low).sum()
219228
n_high_outliers = (high < values).sum()
220229
result = {"n_low_outliers": n_low_outliers, "n_high_outliers": n_high_outliers}
230+
np_histogram_inliers = np_histogram_values[inlier_mask]
221231
result["bin_counts"], result["bin_edges"] = np.histogram(
222-
np_histogram_values[inlier_mask]
232+
np_histogram_inliers, range=_get_safe_hist_range(np_histogram_inliers)
223233
)
224234
if ax is None:
225235
return result
226-
n, bins, patches = ax.hist(values[inlier_mask])
236+
histogram_inliers = values[inlier_mask]
237+
n, bins, patches = ax.hist(
238+
histogram_inliers, range=_get_safe_hist_range(histogram_inliers)
239+
)
227240
n_out = n_low_outliers + n_high_outliers
228241
if not n_out:
229242
return result

skrub/_reporting/tests/test_plotting.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ def test_histogram():
2828
data = pd.Series([0.0])
2929
_, hist = _plotting.histogram(data)
3030
assert (hist["n_low_outliers"], hist["n_high_outliers"]) == (0, 0)
31+
32+
low = np.float32(10.0)
33+
high = np.nextafter(low, 11.0)
34+
data = pd.Series([low, high])
35+
_, hist = _plotting.histogram(data)

0 commit comments

Comments
 (0)