np.fabs
xr.DataArray
在's 但不是's上工作正常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
任何想法如何将其应用于 a xr.Dataset
?
我可以遍历xr.Dataset
(见下文)中的变量,但我不确定是否有更有效的东西
for i, var in enumerate(ds.data_vars):
ds[var] = np.fabs(ds[var])