home / github / issue_comments

Menu
  • GraphQL API
  • Search all tables

issue_comments: 366825366

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/1914#issuecomment-366825366 https://api.github.com/repos/pydata/xarray/issues/1914 366825366 MDEyOklzc3VlQ29tbWVudDM2NjgyNTM2Ng== 6815844 2018-02-19T23:21:05Z 2018-02-19T23:34:58Z MEMBER

I am not sure if it is efficient to interact with a cluster, but I often use MultiIndex for make a cartesian product, ```python In [1]: import xarray as xr ...: import numpy as np ...: data = xr.DataArray(np.full((3, 4), np.nan), dims=('x', 'y'), ...: coords={'x': [0, 1, 2], 'y': ['a', 'b', 'c', 'd']}) ...: ...: data ...: Out[1]: <xarray.DataArray (x: 3, y: 4)> array([[ nan, nan, nan, nan], [ nan, nan, nan, nan], [ nan, nan, nan, nan]]) Coordinates: * x (x) int64 0 1 2 * y (y) <U1 'a' 'b' 'c' 'd'

In [2]: data1 = data.stack(xy=['x', 'y']) ...: data1 ...: Out[2]: <xarray.DataArray (xy: 12)> array([ nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]) Coordinates: * xy (xy) MultiIndex - x (xy) int64 0 0 0 0 1 1 1 1 2 2 2 2 - y (xy) object 'a' 'b' 'c' 'd' 'a' 'b' 'c' 'd' 'a' 'b' 'c' 'd' For the above example, `data` becomes 1-dimensional with coordinate `xy`, where `xy` is a product of `x` and `y`. Each entry of `xy` is tuple of 'x' and 'y' value,python In [3]: data1[0] Out[3]: <xarray.DataArray ()> array(np.nan) Coordinates: xy object (0, 'a') and we can assign a value for given coordinate values by `loc` method,python In [5]: # Assuming we found the result with (1, 'a') is 2.0 ...: data1.loc[(1, 'a'), ] = 2.0

In [6]: data1 Out[6]: <xarray.DataArray (xy: 12)> array([ nan, nan, nan, nan, 2., nan, nan, nan, nan, nan, nan, nan]) Coordinates: * xy (xy) MultiIndex - x (xy) int64 0 0 0 0 1 1 1 1 2 2 2 2 - y (xy) object 'a' 'b' 'c' 'd' 'a' 'b' 'c' 'd' 'a' 'b' 'c' 'd' ```

Note that we need to access via data1.loc[(1, 'a'), ], rather than data1.loc[(1, 'a')] (last comma in the bracket is needed.)

EDIT: I modified my previous comment to take the partial assignment into accout.

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