home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 259272844

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/1072#issuecomment-259272844 https://api.github.com/repos/pydata/xarray/issues/1072 259272844 MDEyOklzc3VlQ29tbWVudDI1OTI3Mjg0NA== 5572303 2016-11-08T21:59:06Z 2016-11-08T22:10:54Z CONTRIBUTOR

So I spent some time looking at dataset._calculate_binary_op but couldn't quite come up with what I wanted. After banging my head against the wall a bit this is what I have:

def combine_first(left, right): la, ra = xr.align(left, right, join='outer', copy=False) # should copy=True? la, ra = la.where(ra.isnull() | ra.notnull()), ra.where(la.isnull() | la.notnull()) ra.values[la.notnull().values] = la.values[la.notnull().values] return ra

And it seems to work. My test cases are

```

l_2d <xarray.DataArray (x: 2, y: 2)> array([[1, 1], [1, 1]]) Coordinates: * x (x) |S1 'a' 'b' * y (y) int64 -2 0 r_2d <xarray.DataArray (x: 2, y: 2)> array([[0, 0], [0, 0]]) Coordinates: * x (x) |S1 'b' 'c' * y (y) int64 0 2 ar_1d <xarray.DataArray (x: 3)> array([4, 5, 6]) Coordinates: * x (x) |S1 'a' 'b' 'd' ```

and here are the results:

```

combine_first(l_2d, r_2d) <xarray.DataArray (x: 3, y: 3)> array([[ 1., 1., nan], [ 1., 1., 0.], [ nan, 0., 0.]]) Coordinates: * x (x) object 'a' 'b' 'c' * y (y) int64 -2 0 2 combine_first(r_2d, l_2d) <xarray.DataArray (x: 3, y: 3)> array([[ 1., 1., nan], [ 1., 0., 0.], [ nan, 0., 0.]]) Coordinates: * x (x) object 'a' 'b' 'c' * y (y) int64 -2 0 2 combine_first(l_2d, ar_1d) <xarray.DataArray (x: 3, y: 2)> array([[ 1., 1.], [ 1., 1.], [ 6., 6.]]) Coordinates: * x (x) object 'a' 'b' 'd' * y (y) int64 -2 0 combine_first(ar_1d, l_2d) <xarray.DataArray (x: 3, y: 2)> array([[ 4., 4.], [ 5., 5.], [ 6., 6.]]) Coordinates: * x (x) object 'a' 'b' 'd' * y (y) int64 -2 0 ```

I don't like the fact that I have to access .values, and the use of .where is slightly wonky. But this is definitely the cleanest working solution I have thus far. Any suggestion to improve is welcome.

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  186680248
Powered by Datasette · Queries took 2.679ms · About: xarray-datasette