python - Xarray沿相同维度添加(如求和)两行但坐标值不同
问题描述
我使用 xarray 创建了两个具有相同尺寸和坐标的不同 DataArray。但是我想在这些维度之一中添加两个不同的坐标。我正在尝试将坐标“a”添加到维度“x”中的坐标“b”。如果这些是我的矩阵的唯一维度,则有一个简单的解决方法,但如果我有更多维度并且我想保持其他维度的正常 xarray 行为,则更复杂。请参阅下面在最后一行失败的示例。我知道如何在 numpy 中手动修复这个问题,但 xarray 的美妙之处在于我不应该这样做。
xarray 是否允许这种操作的简单解决方案?
import xarray as xr
import numpy as np
# create simple DataArray M and N to show what I would like to do
M = xr.DataArray([1, 2], dims="x",coords={'x':['a','b']})
N = xr.DataArray([3, 4], dims="x",coords={'x':['a','b']})
print(M.sel(x='a')+N.sel(x='b')) # this will NOT give me the value
print(M.sel(x='a').values+N.sel(x='b').values) # this will give me the value
# create a more complex DataArray M and N to show what the challenge
m = np.arange(3*2*4)
m = m.reshape(3,2,4)
n = np.arange(4*2*3)
n = n.reshape(4,2,3)
M = xr.DataArray(m, dims=['z1',"x","z2"],coords={'x':['a','b']})
N = xr.DataArray(n, dims=["z2",'x','z1'],coords={'x':['a','b']})
print(M.sel(x='a')+N.sel(x='b')) # this will NOT give me the value
print(M.sel(x='a').values+N.sel(x='b').values) # this will result in an error
解决方案
推荐阅读
- javascript - 使用 promise 模块连接 mongodb 时出错
- google-apps-script - 使用 Google Apps 脚本为 Google Docs 中表格单元格的第一行设置样式
- java - 尽管在 spring-boot-maven-plugin 中配置了排除,但日志依赖项仍包含在 lib 文件夹中
- oracle - Oracle 数据库 12c“压缩查询”选项
- ios - 在 iOS 的启动屏幕上显示两张图片
- objective-c - DTCoreText 没有这样的文件或目录 libDTCoreText.a
- java - 如何接收请求正文数据以及多部分图像文件?
- c# - 如何将不同面板中存在的单选按钮分组为一个组?
- php - 在 nginx 上部署 PHP 和 react js 应用
- json - Google Apps 脚本 - 如何将 JSON 数据流式传输到 BigQuery?