| 1614 | if dim is not None: |
| 1615 | |
| 1616 | def np_cov_ind(ts1, ts2, a, x): |
| 1617 | # Ensure the ts are aligned and missing values ignored |
| 1618 | ts1, ts2 = broadcast(ts1, ts2) |
| 1619 | valid_values = ts1.notnull() & ts2.notnull() |
| 1620 | |
| 1621 | # While dropping isn't ideal here, numpy will return nan |
| 1622 | # if any segment contains a NaN. |
| 1623 | ts1 = ts1.where(valid_values) |
| 1624 | ts2 = ts2.where(valid_values) |
| 1625 | |
| 1626 | return np.ma.cov( |
| 1627 | np.ma.masked_invalid(ts1.sel(a=a, x=x).data.flatten()), |
| 1628 | np.ma.masked_invalid(ts2.sel(a=a, x=x).data.flatten()), |
| 1629 | ddof=ddof, |
| 1630 | )[0, 1] |
| 1631 | |
| 1632 | expected = np.zeros((3, 4)) |
| 1633 | for a in [0, 1, 2]: |