Skip to content

Commit 507e94a

Browse files
committed
fix: histogram safe error
1 parent 4cdb70d commit 507e94a

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

src/ydata_profiling/model/summary_algorithms.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,21 @@ def histogram_compute(
4040
hist_config = config.plot.histogram
4141
bins_arg = "auto" if hist_config.bins == 0 else min(hist_config.bins, n_unique)
4242

43-
# --- FIX para NumPy 2.x: fallback seguro quando o range é demasiado pequeno ---
44-
try:
45-
bins = np.histogram_bin_edges(finite_values, bins=bins_arg)
46-
except ValueError as exc:
47-
# NumPy 2.x error: "Too many bins for data range. Cannot create X finite-sized bins."
48-
if "Too many bins for data range" in str(exc):
49-
# fallback robusto: deixar NumPy escolher automaticamente
50-
bins = np.histogram_bin_edges(finite_values, bins="auto")
51-
else:
52-
# manter comportamento anterior para erros diferentes
43+
def _safe_histogram_bin_edges(values: np.ndarray, bins_param: Union[int, str]) -> np.ndarray:
44+
try:
45+
return np.histogram_bin_edges(values, bins=bins_param)
46+
except ValueError as exc:
47+
if "Too many bins for data range" in str(exc):
48+
# fallback: auto selection
49+
return np.histogram_bin_edges(values, bins="auto")
5350
raise
5451

55-
# manter a lógica original do max_bins
52+
bins = _safe_histogram_bin_edges(finite_values, bins_arg)
53+
5654
if len(bins) > hist_config.max_bins:
57-
bins = np.histogram_bin_edges(finite_values, bins=hist_config.max_bins)
58-
weights = weights if weights is not None and len(weights) == hist_config.max_bins else None
55+
bins = _safe_histogram_bin_edges(finite_values, hist_config.max_bins)
56+
if weights is not None and len(weights) != len(bins):
57+
weights = None
5958

6059
stats[name] = np.histogram(
6160
finite_values,

0 commit comments

Comments
 (0)