python - 如何将数组中的零转换为Nan?

标签 python arrays numpy indexing python-xarray

enter image description here

我使用了temp[temp==0] = np.nan,但我得到了这个错误:

IndexError: 2-dimensional boolean indexing is not supported.

最佳答案

我会使用where,以避免必须下拉到numpy:

In [35]: d
Out[35]: 
<xarray.DataArray (dim_0: 2, dim_1: 3)>
array([[0, 1, 2],
       [3, 4, 5]])
Dimensions without coordinates: dim_0, dim_1

In [36]: d.where(d != 0)
Out[36]: 
<xarray.DataArray (dim_0: 2, dim_1: 3)>
array([[nan,  1.,  2.],
       [ 3.,  4.,  5.]])
Dimensions without coordinates: dim_0, dim_1

如有必要,它将自动移动到 float 。

关于python - 如何将数组中的零转换为Nan?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52630771/

相关文章:

java - 在可从主 Java 访问的静态方法内创建数组

python - ValueError : could not broadcast input array from shape (110, 110,3) 变成形状 (110,110)

python - 如何获得 NumPy 数组的描述性统计信息?

python 扭曲 : restricting access by IP address

javascript - 如何将对象数组中的字段值复制到另一个数组中

ios - 在 for-in 循环中更改数组的名称

python - 将 ctypes 指针转换为 float 的 numpy 数组

python - 计算不同类型的支出 - Pandas/Numpy - Python

python - 为什么 cv2.imwrite() 会改变图片的颜色?

python - 为什么 Python 的列表推导式不复制参数以便实际对象不能被改变?