hard to understand behavior of concat #5072
-
I have two datasets (one main dataset, with a second dataset which I want to append to it) and want to concatenate them along one axis. It seems like a straightforward call to xr.concat(), but I'm getting a result that I find hard to understand. Here is the main dataset "dsnam"
(Although it looks like the main variables u10m and v10m are all NaNs, they aren't-- only the beginning/ending groups of them are.) Here is the smaller dataset I want to append "dsnamnew":
I want to concatenate along the dimension "tZ" so I make this call:
And here is the result "both":
All aspects are just the way I expected ... except that Lat and Lon are now (tZ,Yind,Xind) instead of (Yind,Xind). Why would this happen? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@DanCodigaMWRA
You want to treat
In [13]: ds
Out[13]:
<xarray.Dataset>
Dimensions: (Xind: 2, Yind: 3, tZ: 3)
Coordinates:
* Xind (Xind) int64 0 1
* Yind (Yind) int64 0 1 2
* tZ (tZ) datetime64[ns] 2021-01-01 2021-02-01 2021-03-01
Data variables:
Lat (Xind, Yind) int64 0 1 2 3 4 5
u10m (tZ, Xind, Yind) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
In [15]: xr.concat([ds, ds], dim='tZ', data_vars=['u10m'])
Out[15]:
<xarray.Dataset>
Dimensions: (Xind: 2, Yind: 3, tZ: 6)
Coordinates:
* Xind (Xind) int64 0 1
* Yind (Yind) int64 0 1 2
* tZ (tZ) datetime64[ns] 2021-01-01 2021-02-01 ... 2021-02-01 2021-03-01
Data variables:
Lat (Xind, Yind) int64 0 1 2 3 4 5
u10m (tZ, Xind, Yind) int64 0 1 2 3 4 5 6 7 ... 10 11 12 13 14 15 16 17
In [16]: xr.concat([ds.set_coords(['Lat']), ds.set_coords(['Lat'])], dim='tZ')
Out[16]:
<xarray.Dataset>
Dimensions: (Xind: 2, Yind: 3, tZ: 6)
Coordinates:
* Xind (Xind) int64 0 1
* Yind (Yind) int64 0 1 2
* tZ (tZ) datetime64[ns] 2021-01-01 2021-02-01 ... 2021-02-01 2021-03-01
Lat (Xind, Yind) int64 0 1 2 3 4 5
Data variables:
u10m (tZ, Xind, Yind) int64 0 1 2 3 4 5 6 7 ... 10 11 12 13 14 15 16 17 I hope this helps |
Beta Was this translation helpful? Give feedback.
@DanCodigaMWRA
You want to treat
Lat
andLon
as coordinates during the concatenation, however with your current setup, xarray isn't aware of this fact. There are two remedies to this.data_vars
i.e.xr.concat(...., data_vars=['v10m, 'u10m', ...])