问题描述
我有一个包含int
s 的数据集,我想根据一些标准选择一个子数据集,但我想保留整数数据类型。在我看来,Xarray 强制将整数数据更改为浮点数据类型。
示例设置
代码
import numpy
import xarray
nums = numpy.random.randint(0, 100, 13)
names = numpy.random.choice(["babadook", "samara", "jason"], 13)
data_vars = {"num": xarray.DataArray(nums), "name": xarray.DataArray(names)}
dataset = xarray.Dataset(data_vars)
print(dataset)
输出
<xarray.Dataset>
Dimensions: (dim_0: 13)
Coordinates:
* dim_0 (dim_0) int64 0 1 2 3 4 5 6 7 8 9 10 11 12
Data variables:
num (dim_0) int64 93 99 49 35 92 14 41 57 28 59 74 1 15
name (dim_0) <U8 'babadook' 'samara' 'samara' 'samara' 'jason' ...
In [16]:
示例问题
代码
subdataset = dataset.where(dataset.num < 50, drop=True)
print(subdataset)
输出
<xarray.Dataset>
Dimensions: (dim_0: 7)
Coordinates:
* dim_0 (dim_0) int64 2 3 5 6 8 11 12
Data variables:
num (dim_0) float64 49.0 35.0 14.0 41.0 28.0 1.0 15.0
name (dim_0) <U32 'samara' 'samara' 'jason' 'babadook' 'jason' ...