Conversation
_mean documents returning None when there is nothing to average, but it does not drop None values the way _stdev and _pstdev do, so a score list containing an unmeasured entry raised TypeError from statistics.mean instead of averaging the scores that were measured.
chrikrah
approved these changes
Sep 30, 2026
chrikrah
left a comment
There was a problem hiding this comment.
@sclfcz I would merge this. The note below is non-blocking.
non-blocking: real callers aggregate a pandas column, so a missing score arrives as NaN, not None, and _stdev raises before _mean is reached. get_mean_grouping with one blank cct-accuracy cell fails identically before and after this change:
$ python probe_group.py # 3 rows, cct-accuracy [0.9, None, 0.8], group_by="doctype"; torch and unstructured_inference stubbed
main ddf4453: raised: ValueError inf or nan encountered in data
PR 43867d0: raised: ValueError inf or nan encountered in data
# Python 3.13.15, pandas 2.3.3
Would you like pd.notna in _mean, _stdev and _pstdev as part of this change, or kept for a follow-up?
This branch has not been deployed
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.
Fixes #4509
What
_meandocuments returningNonewhen there is nothing to average, but unlike its neighbours_stdevand_pstdevit does not dropNonevalues before averaging, so a score list with an unmeasured entry raised instead of being averaged:_mean([0.9, None, 0.8])TypeError: can't convert type 'NoneType' to numerator/denominator0.85_mean([None, None])TypeErrorNone_mean([])None_mean([1.0, 2.0])1.5_stdev([0.9, None, 0.8])→0.071and_pstdev→0.05already filter those values, and their type hint isList[Optional[float]];_meannow matches them (hint included).Testing
Added
test_mean_ignores_unmeasured_scorestotest_unstructured/metrics/test_utils.pycovering the four rows above.pytest test_unstructured/metrics/test_utils.py→ 6 passed; reverting the filter fails exactly the new test and leaves the other five passing.