Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dask-friendly nan check in xr.corr() and xr.cov() #5284

Merged
merged 10 commits into from
May 27, 2021
19 changes: 15 additions & 4 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,12 +1326,23 @@ def _cov_corr(da_a, da_b, dim=None, ddof=0, method=None):

# 2. Ignore the nans
valid_values = da_a.notnull() & da_b.notnull()
valid_count = valid_values.sum(dim) - ddof

if not valid_values.all():
da_a = da_a.where(valid_values)
da_b = da_b.where(valid_values)
def _get_valid_values(da, other):
"""
Function to lazily mask da_a and da_b
following a similar approach to
https://github.com/pydata/xarray/pull/4559
"""
missing_vals = np.logical_or(da.isnull(), other.isnull())
if missing_vals.any():
da = da.where(~missing_vals)
return da
else:
return da

valid_count = valid_values.sum(dim) - ddof
da_a = da_a.map_blocks(_get_valid_values, args=[da_b])
da_b = da_b.map_blocks(_get_valid_values, args=[da_a])

# 3. Detrend along the given dim
demeaned_da_a = da_a - da_a.mean(dim=dim)
Expand Down