Skip to content

Simplify missing value handling in xarray.corr #6025

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

Merged
merged 1 commit into from
Nov 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,25 +1346,10 @@ 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()
da_a = da_a.where(valid_values)
da_b = da_b.where(valid_values)
valid_count = valid_values.sum(dim) - ddof

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:
# ensure consistent return dtype
return da.astype(float)

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)
demeaned_da_b = da_b - da_b.mean(dim=dim)
Expand Down