Repository navigation
Fix var and std on object arrays - #11987
Open
parabol123654 wants to merge 1 commit into
Open
parabol123654 wants to merge 1 commit into
parabol123654 wants to merge 1 commit into
Conversation
For object dtype, var goes through _nanvar_object, which computes the
mean with keepdims=True so it can be subtracted from the data. But
_nanmean_ddof_object only passed keepdims to the sum, not to the count
of valid values, so the sum (kept dims) was divided by a count without
them. Reducing the last axis of a 2-D array then broadcast wrongly:
v = np.array([[1, 2], [10, np.nan]], dtype=object)
xr.DataArray(v, dims=("y", "x")).var("x") # [0.625, 25.], not [0.25, 0.]
and with a non-square array it raised a broadcasting ValueError. This
goes back to the nanops refactor in pydata#2236.
std had no object branch at all and called np.nanstd, which fails with
"loop of ufunc does not support argument 0 of type float which has no
callable sqrt method" when the array contains NaN.
Pass keepdims through to count, and compute std on object arrays as
the square root of _nanvar_object.
Co-authored-by: Claude <noreply@anthropic.com>
parabol123654
force-pushed
the
fix-object-var-std
branch
from
December 4, 2026 18:13
1b6cddf to
34755db
Compare
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.
Description
varon an object array gives wrong numbers when reducing the last axis of a 2-D array: for[[1, 2], [10, nan]]as object,var("x")returns[0.625, 25.]instead of[0.25, 0.](and a non-square array raises a broadcasting error)._nanmean_ddof_objectis called withkeepdims=Truefrom_nanvar_object, but the valid count was computed without it, so the division broadcast against the wrong axis. I passkeepdimsthrough tocount.stdon object arrays with NaN raisedTypeError(no callablesqrt) since it went straight tonp.nanstd; it now takes the square root of the object variance, likenanvaralready does for object dtype.The new test compares object and float results for both reductions, every dim and
ddof0/1, with numpy and dask; 14 of the 24 cases fail on main.test_duck_array_ops,test_dataarray,test_dataset,test_rolling,test_weightedandtest_groupbypass.Checklist
whats-new.rstAI Disclosure
Tools: Claude Code. It found the bug while comparing object and float reductions and wrote the fix and test; I reproduced it, read the change and reran the tests myself.
[This is Claude Code on behalf of Manh Nguyen]