【发布时间】:2017-10-27 17:49:24
【问题描述】:
np.fabs 在 xr.DataArray 上运行良好,但在 xr.Dataset 上运行良好。
data = xr.DataArray(np.random.randn(2, 3), coords={'x': ['a', 'b']}, dims=('x', 'y'))
ds = xr.Dataset({'foo': data, 'bar': ('x', [1, 2]), 'baz': np.pi})
np.fabs(ds)
TypeError: ufunc 'fabs' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
np.fabs(ds['foo'])
<xarray.DataArray 'foo' (x: 2, y: 3)>
array([[ 0.384305, 0.161676, 0.07573 ],
[ 0.789885, 1.299188, 1.965528]])
Coordinates:
* x (x) <U1 'a' 'b'
Dimensions without coordinates: y
任何想法如何将其应用于xr.Dataset?
我可以遍历 xr.Dataset 中的变量(见下文),但我不确定是否有更有效的方法
for i, var in enumerate(ds.data_vars):
ds[var] = np.fabs(ds[var])
【问题讨论】: