FYI: Xarray's .sum() will convert nan to 0 if all values are nan in a slice, which can affect comparison with CDAT summing results
#612
tomvothecoder
started this conversation in
4. General
Replies: 2 comments 4 replies
-
|
Just to make sure I understand: If an array has |
Beta Was this translation helpful? Give feedback.
3 replies
-
|
Yet another example of what happens when you rely on NaNs, rather than on masks ? Though I confess that the documentation of ma.MaskedArray.sum is not very detailed. You need to know that the operations will only take the non-masked elements into account, but this is also what you expect |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Overview
I've ran into this a few times while migrating production code for
e3sm_diagsande3sm_to_cmipfrom CDAT to Xarray/xCDATI compare two netCDF, one from Xarray and one from CDAT (post-summing) using
np.testing.assert_all_close(). However, I getx and y nan location mismatchbecause Xarray replacesnanwith 0s while CDAT maintainsnan(if all values arenanfor a slice).Example:
Reason why Xarray's
.sum()replacesnanwith 0Workaround (if you need to compare CDAT vs. Xarray/xCDAT sum results)
skipnaandmin_countarg, for example.sum(..., skipna=True, min_count=1).np.sum()directly on the.valuesof thexr.DataArrayand rebuild thexr.DataArrayobject with those results (example).Related posts:
Beta Was this translation helpful? Give feedback.
All reactions