Skip to content

Commit c1c9a77

Browse files
committed
fix: change the logic to align with the typing
1 parent 138eccd commit c1c9a77

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

src/ydata_profiling/model/summary_algorithms.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,34 @@ def histogram_compute(
109109
return stats
110110

111111
def chi_square(
112-
values: Optional[np.ndarray] = None, histogram: Optional[np.ndarray] = None
112+
values: Optional[np.ndarray] = None,
113+
histogram: Optional[np.ndarray] = None,
113114
) -> dict:
115+
# Case 1: histogram not passed → we compute it
114116
if histogram is None:
117+
if values is None:
118+
return {"statistic": 0, "pvalue": 0}
119+
120+
# Try NumPy "auto" binning (may fail under NumPy 2)
115121
try:
116122
bins = np.histogram_bin_edges(values, bins="auto")
117-
except ValueError as exc:
118-
# NumPy 2.x strict binning error
119-
if "Too many bins for data range" in str(exc) or "Cannot create" in str(exc):
120-
# fallback: 1 bin covering the full range
121-
finite = values[np.isfinite(values)]
122-
if finite.size == 0:
123-
return {"statistic": 0, "pvalue": 0}
124-
vmin = float(np.min(finite))
125-
vmax = float(np.max(finite))
126-
if vmin == vmax:
127-
# degenerate range, expand a little
128-
eps = 0.5 if vmin == 0 else abs(vmin) * 0.1
129-
bins = np.array([vmin - eps, vmin + eps])
130-
else:
131-
bins = np.array([vmin, vmax])
123+
except ValueError:
124+
# Fallback: basic 1-bin histogram covering the min→max range
125+
finite = values[np.isfinite(values)]
126+
if finite.size == 0:
127+
return {"statistic": 0, "pvalue": 0}
128+
129+
vmin = float(finite.min())
130+
vmax = float(finite.max())
131+
if vmin == vmax:
132+
bins = np.array([vmin - 0.5, vmin + 0.5])
132133
else:
133-
raise
134+
bins = np.array([vmin, vmax])
134135

135136
histogram, _ = np.histogram(values, bins=bins)
136137

137-
if len(histogram) == 0 or np.sum(histogram) == 0:
138+
# Case 2: histogram exists but is empty
139+
if histogram.size == 0 or histogram.sum() == 0:
138140
return {"statistic": 0, "pvalue": 0}
139141

140142
return dict(chisquare(histogram)._asdict())

0 commit comments

Comments
 (0)