简体   繁体   English

如何在DataArray(xarray)中合并两个坐标值?

[英]How to combine two coordinate values in a DataArray (xarray)?

I have an xarray.Dataarray with 4 coordinates: fp, station, run_date, elnu 我有一个带有4个坐标的xarray.Dataarrayfp, station, run_date, elnu

Dimensions are currently (same order): ( 1 , 2 , 3261 , 417 ) 尺寸是目前(相同顺序):( 123261417

Station has the values "101470" and "108700", want to put these two together to have a dimension of ( 1 , 1 , 3261*2 , 417 ) afterwards, I kind of want to reshape them. 车站的值有“101470”和“108700”,希望把这两个共同拥有的(尺寸113261*2417 )之后,我有种想重塑他们。 Problem is, I can't figure out how to do that and how to solve the problem that they would then have the same coordinates (if I would act like they would have the same station). 问题是,我无法弄清楚该如何做以及如何解决它们将具有相同坐标的问题(如果我认为它们将具有相同的测站)。

Maybe I just don't find the right words to search for on google (not native English). 也许我只是找不到在Google上搜索的正确单词(不是英语)。 Appreciate any help. 感谢任何帮助。

The functionality you're looking for is in xarray's .stack() method. 您正在寻找的功能在xarray的.stack()方法中。 Example: 例:

>>> import xarray

>>> import numpy as np

>>> da = xarray.DataArray(np.zeros((1, 2, 3261, 417)), dims=['fp', 'station', 'run_date', 'elnu'])

>>> da
<xarray.DataArray (fp: 1, station: 2, run_date: 3261, elnu: 417)>
array([[[[ 0., ...,  0.],
         ..., 
         [ 0., ...,  0.]],

        [[ 0., ...,  0.],
         ..., 
         [ 0., ...,  0.]]]])
Dimensions without coordinates: fp, station, run_date, elnu

>>> da.stack(station_date=['station', 'run_date'])
<xarray.DataArray (fp: 1, elnu: 417, station_date: 6522)>
array([[[ 0.,  0., ...,  0.,  0.],
        [ 0.,  0., ...,  0.,  0.],
        ..., 
        [ 0.,  0., ...,  0.,  0.],
        [ 0.,  0., ...,  0.,  0.]]])
Coordinates:
  * station_date  (station_date) MultiIndex
  - station       (station_date) int64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
  - run_date      (station_date) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
Dimensions without coordinates: fp, elnu

Follow up with .transpose() and/or .expand_dims() to reorder dimensions to your desired order. 跟着.expand_dims() .transpose()和/或.expand_dims()将尺寸重新排序为所需的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM