MAINT Minor refactor to de-indent majority of `_assert_all_finite`
@@ -99,12 +99,20 @@ def _assert_all_finite(
if _get_config()["assume_finite"]:
return
X = np.asanyarray(X)
+
+ # for object dtype data, we only check for NaNs (GH-13254)
+ if X.dtype == np.dtype("object") and not allow_nan:
+ if _object_dtype_isnan(X).any():
+ raise ValueError("Input contains NaN")
+
+ # We need only consider float arrays, hence can early return for all else.
+ if X.dtype.kind not in "fc":
+ return
+
# First try an O(n) time, O(1) space solution for the common case that
# everything is finite; fall back to O(n) space `np.isinf/isnan` or custom
# Cython implementation to prevent false positives and provide a detailed
# error message.
- is_float = X.dtype.kind in "fc"
- if is_float:
with np.errstate(over="ignore"):
first_pass_isfinite = np.isfinite(np.sum(X))
if first_pass_isfinite:
@@ -145,11 +153,6 @@ def _assert_all_finite(
)
raise ValueError(msg_err)
- # for object dtype data, we only check for NaNs (GH-13254)
- elif X.dtype == np.dtype("object") and not allow_nan:
- if _object_dtype_isnan(X).any():
- raise ValueError("Input contains NaN")
-
def assert_all_finite(
X,
