Xarray:是否有一种简洁的方法可以减少整个数据集尺寸x和y,而无需更改变量的数据类型?
我正在尝试将我的Xarray数据集维度(X和Y)减少一个,例如257x257至256x256。
该代码是我尝试过的:
if cube.dims['x'] > patch_size:
cube=cube.where((cube.y<cube.y.data.max()) & (cube.x<cube.x.max()),drop=True)
如果cube.dims ['x']&gt; patch_size: cube.y&lt; cube.y.data.max())
cube = cube.Where( ( 对Float64的数据类型(可能是因为在选择数据时(其中()语句在选择数据时转换为“ NAN”)。 是否有更好的方法可以在不将数据类型更改为Float64的情况下?
I'm trying to reduce my xarray dataset dimension (x and y) by one, e.g. 257x257 to 256x256.
This code is what I have tried:
if cube.dims['x'] > patch_size:
cube=cube.where((cube.y<cube.y.data.max()) & (cube.x<cube.x.max()),drop=True)
if cube.dims['x'] > patch_size:
cube=cube.where((cube.y<cube.y.data.max()) & (cube.x<cube.x.max()),drop=True)
When I run this code, all variables change the data type to float64 (probably because where() statement converts to "nan" while selecting data).
Is there a better way of doing this without changing the data type to float64?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与其掩盖和丢弃数据的位置,只需在X和Y尺寸沿X和Y尺寸选择所需的数据即可。这将比
掩盖,并且将围绕您看到的
int-&gt; float
键入促销问题。您可以做多种方法,例如 .SEL 对于基于标签的索引或
.isel
用于位置索引。由于您尝试提取除最后一个索引以外的所有索引,因此我将使用iSel
slice :也可以使用SEL来完成,就像您一样使用过滤的坐标您的问题:
这两种方法都可以与数据库和数据集一起使用。
如果您正在使用数据阵列,则也可以通过用
.loc
- 在执行此操作时要小心,因为Xarray并不总是保留尺寸排序:指定订单的尺寸不允许这样的数据集。
请参阅 xarray docs on Indexing and Selecting Data 以获取更多信息关于这个话题。
请注意类型促销的注意。
在上面的特定情况下,您的特定情况会更好地表现出色,但您提出了一个有趣的问题,我认为这是
dataarray.were where
的不必要的限制。我认为没有理由无法在使用Where
使用drop = true
时无法设置custom其他
填充值。目前,Xarray的主张禁止这一点 - 我在这里提出了一个问题,以查看是否可以解决此问题: gh#6466 。Rather than masking and dropping the data with where, simply select the data you want along the x and y dimensions. This will be faster than masking with
where
, and will get around theint->float
type promotion problem you're seeing.You could do this a number of ways, e.g. with
.sel
for label-based indexing or.isel
for positional indexing. Since you're trying to extract all but the last indices, I'll useisel
withslice
:This could also be done with sel, using filtered coordinates as you have in your question:
Both of these methods will work with both DataArray and Datasets.
If you're working with a DataArray, you could also do this by referring to the dimensions positionally with
.loc
- just be careful when doing this because xarray doesn't always preserve dimension ordering:Referring to dimensions by order like this is not allowed for datasets.
See the xarray docs on Indexing and Selecting Data for more info on this topic.
Note on type promotion with .where
While the above is going to perform better for your specific case, you've raised an interesting issue about what I think is an unnecessary limitation of
DataArray.where
. I think there's no reason why you couldn't set a customother
fill value when using.where
withdrop=True
. Currently, this is prohibited by an assertion in xarray - I've raised an issue here to see if we can fix this: gh#6466.