From 663d3810970d40552648c8bb77592f1e57fac28c Mon Sep 17 00:00:00 2001 From: ruus77 Date: Mon, 30 Nov 2026 12:26:16 +0100 Subject: [PATCH] Use NumPy min/max to validate categorical indices --- CHANGELOG.md | 6 ++++++ src/tabmat/categorical_matrix.py | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cd24d03..f599fcae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ https://semver.org/spec/v2.0.0.html. The most important bits are: # Changelog +## 4.3.3 - unreleased + +**Other changes:** + +- [`tabmat.CategoricalMatrix`][tabmat.CategoricalMatrix] validates indices passed together with `categories` with NumPy's `min` and `max` instead of Python's built-in functions, which makes construction faster for large inputs. + ## 4.3.2 - 2026-11-20 **Bug fix:** diff --git a/src/tabmat/categorical_matrix.py b/src/tabmat/categorical_matrix.py index 7a234c4e..75943eb3 100644 --- a/src/tabmat/categorical_matrix.py +++ b/src/tabmat/categorical_matrix.py @@ -375,9 +375,9 @@ def __init__( if categories is not None: self.categories = categories indices = np.nan_to_num(cat_vec, nan=-1) - if max(indices) >= len(categories): + if indices.max() >= len(categories): raise ValueError("Indices exceed length of categories.") - if min(indices) < -1: + if indices.min() < -1: raise ValueError("Indices must be non-negative (or -1 for missing).") else: indices, self.categories = _extract_codes_and_categories(cat_vec)