Add Comparator equality for pathlib, numpy arrays, and pandas objects#1115
Open
ghostiee-11 wants to merge 2 commits intoholoviz:mainfrom
Open
Add Comparator equality for pathlib, numpy arrays, and pandas objects#1115ghostiee-11 wants to merge 2 commits intoholoviz:mainfrom
ghostiee-11 wants to merge 2 commits intoholoviz:mainfrom
Conversation
Extends Comparator.equalities with support for pathlib.PurePath (using operator.eq), numpy arrays (using np.array_equal with dtype and shape checks), and pandas DataFrame/Series (using .equals()). Large arrays and frames above array_max_size (default 1M elements) skip comparison and return False to avoid expensive element-wise checks. Adds 38 tests covering equal, not-equal, shape mismatch, dtype mismatch, NaN handling, identity, and size cutoff behavior. Closes holoviz#902
There was a problem hiding this comment.
Pull request overview
Adds richer equality semantics to param.parameterized.Comparator.is_equal so watcher triggering better reflects “no real change” for common scientific/python types, addressing #902.
Changes:
- Add Comparator equality handling for
pathlib.PurePath, numpy array-like objects, and pandas objects with size-based cutoffs to avoid expensive comparisons. - Introduce
Comparator.array_max_sizethreshold (default 1,000,000 elements) to bail out of large numpy/pandas comparisons. - Expand
tests/testcomparator.pycoverage for the newly supported types and cutoff behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
param/parameterized.py |
Extends Comparator with pathlib + numpy/pandas equality logic and introduces array_max_size cutoff. |
tests/testcomparator.py |
Adds new parametrized and targeted tests for pathlib/numpy/pandas comparator behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+2124
to
+2128
| def _array_equal(obj1, obj2): | ||
| """Equality check for numpy arrays with a size cutoff.""" | ||
| import numpy as np | ||
| if obj1 is obj2: | ||
| return True |
param/parameterized.py
Outdated
Comment on lines
+2116
to
+2117
| lambda o: type(o).__module__.startswith('numpy') and hasattr(o, 'shape'): lambda a, b: Comparator._array_equal(a, b), | ||
| lambda o: type(o).__module__.startswith('pandas') and hasattr(o, 'equals'): lambda a, b: Comparator._pandas_equal(a, b), |
| b = pd.Series(range(10)) | ||
| assert not Comparator.is_equal(a, b) | ||
| finally: | ||
| Comparator.array_max_size = old |
…dd DataFrame cutoff test - Tighten numpy lambda to require shape, dtype, and size attributes - Tighten pandas lambda to require shape, size, and equals attributes - Move identity and type checks before numpy import in _array_equal - Catch ImportError in _array_equal in case numpy is not available - Wrap attribute access (shape, dtype, size) in try/except AttributeError - Add test_dataframe_large_skips for DataFrame size cutoff coverage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Comparator.is_equallacks support for common types like numpy arrays, pandas DataFrames/Series, and pathlib paths. Assigning an identical array to a parameter always triggers watchers because the comparator falls through toreturn False.This PR adds equality support for:
operator.eqnp.array_equalwith shape/dtype pre-checks.equals()with shape pre-checkTo avoid expensive element-wise comparisons on large data, both numpy and pandas paths bail out (return
False) when the object has more thanarray_max_sizeelements (default 1,000,000). Better to trigger an unnecessary watcher than to block on a million-element comparison.The numpy and pandas checks use lambda predicates (
type(o).__module__.startswith(...)) so neither library is imported at module level.Closes #902
How Has This Been Tested?
38 tests in
tests/testcomparator.py:pytest tests/testcomparator.py -v # 38 passedChecklist