展开尺寸xarray [英] Expand dimensions xarray

查看:93
本文介绍了展开尺寸xarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在扩展xarray.DataArray对象的尺寸(和坐标)的方法或方法?

Is there an existing method or approach to expand the dimensions (and coordinates) of an xarray.DataArray object?

我想获得类似于 np.expand_dims ,同时为新的扩展DataArray定义新的尺寸和坐标变量.

I would like to obtain something similar to np.expand_dims while at the same time defining a new dimension and coordinate variable for the new expanded DataArray.

使用DataArray.assign_coords()我可以创建一个新的坐标变量,但数组本身不会使用新轴展开.

Using DataArray.assign_coords() I can create a new coordinate variable but the array itself is not expanded with a new axis.

推荐答案

在xarray v0.10.0中,我使用了assign_coords()expand_dims()的组合来添加新的尺寸和坐标变量.

In xarray v0.10.0, I use a combination of assign_coords() and expand_dims() to add a new dimension and coordinate variable.

例如:

import xarray as xr
import numpy as np
data = xr.DataArray([1, 2, 3], dims='x', coords={'x': [10, 20, 30]})
data_newcoord = data.assign_coords(y='coord_value')
data_expanded = data_newcoord.expand_dims('y')
print(data_expanded)
# <xarray.DataArray (y: 1, x: 3)>
# array([[1, 2, 3]])
# Coordinates:
#   * x        (x) int64 10 20 30
#   * y        (y) <U11 'coord_value'

这篇关于展开尺寸xarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆