Skip to content

Commit 23d345b

Browse files
authored
Simplify missing value handling in xarray.corr (#6025)
1 parent 135a335 commit 23d345b

File tree

1 file changed

+2
-17
lines changed

1 file changed

+2
-17
lines changed

xarray/core/computation.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -1346,25 +1346,10 @@ def _cov_corr(da_a, da_b, dim=None, ddof=0, method=None):
13461346

13471347
# 2. Ignore the nans
13481348
valid_values = da_a.notnull() & da_b.notnull()
1349+
da_a = da_a.where(valid_values)
1350+
da_b = da_b.where(valid_values)
13491351
valid_count = valid_values.sum(dim) - ddof
13501352

1351-
def _get_valid_values(da, other):
1352-
"""
1353-
Function to lazily mask da_a and da_b
1354-
following a similar approach to
1355-
https://github.com/pydata/xarray/pull/4559
1356-
"""
1357-
missing_vals = np.logical_or(da.isnull(), other.isnull())
1358-
if missing_vals.any():
1359-
da = da.where(~missing_vals)
1360-
return da
1361-
else:
1362-
# ensure consistent return dtype
1363-
return da.astype(float)
1364-
1365-
da_a = da_a.map_blocks(_get_valid_values, args=[da_b])
1366-
da_b = da_b.map_blocks(_get_valid_values, args=[da_a])
1367-
13681353
# 3. Detrend along the given dim
13691354
demeaned_da_a = da_a - da_a.mean(dim=dim)
13701355
demeaned_da_b = da_b - da_b.mean(dim=dim)

0 commit comments

Comments
 (0)