home / github / issue_comments

Menu
  • GraphQL API
  • Search all tables

issue_comments: 410420312

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/2340#issuecomment-410420312 https://api.github.com/repos/pydata/xarray/issues/2340 410420312 MDEyOklzc3VlQ29tbWVudDQxMDQyMDMxMg== 90008 2018-08-04T03:39:49Z 2018-08-04T03:39:49Z CONTRIBUTOR

@shoyer Thank you for your first detailed response. It definitely took me a while to understand all the intricacies of your words.

In fact, if you do use a Dataset, as opposed to a DataArray, you are able to expand dims for multiple simultaneous arrays together.

For my particular case, a Dataset is probably more appropriate, but I liked the fact that the DataArray had one of the sets as the main set and that I could do math on that one.

Here is an example showing how if you use a Dataset, the behaviour I expect is obtained:

```python

%%

import xarray as xa import numpy as np

n = np.zeros((3, 2))

data = xa.DataArray(n, dims=['y', 'x'], coords={'y':range(3), 'x':range(2)})

z=xa.DataArray(np.arange(6).reshape((3, 2)), dims=['y', 'x'])

my_set = xa.Dataset({'mydata':data, 'z':z})

print('Original Data') print('=============') print(my_set)

%%

my_slice = my_set[{'x': 0, 'y':1}] print("Sliced data") print("===========") print("z coordinate remembers it's own x value") print(f'x = {my_slice.z.x}')

%%

expanded_slice = my_slice.expand_dims('x') print("expanded slice") print("==============") print("forgot that 'z' had 'x' coordinates") print("but remembered it had a 'y' coordinate") print(f"z = {expanded_slice.z}") print(expanded_slice.z.x) ``` Output:

python Original Data ============= <xarray.Dataset> Dimensions: (x: 2, y: 3, z2: 0) Coordinates: * y (y) int64 0 1 2 * x (x) int64 0 1 * z2 (z2) float64 Data variables: mydata (y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 z (y, x) int32 0 1 2 3 4 5 Sliced data =========== z coordinate remembers it's own x value x = <xarray.DataArray 'x' ()> array(0, dtype=int64) Coordinates: y int64 1 x int64 0 expanded slice ============== z = <xarray.DataArray 'z' (x: 1)> array([2]) Coordinates: y int64 1 * x (x) int64 0 <xarray.DataArray 'x' (x: 1)> array([0], dtype=int64) Coordinates: y int64 1 * x (x) int64 0

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