【问题标题】:How to convert Zero to Nan in the array?如何将数组中的零转换为南?
【发布时间】:2018-10-03 15:35:05
【问题描述】:

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

IndexError:不支持二维布尔索引。

【问题讨论】:

  • 能否请您告诉我们您尝试了什么以及您想要什么。我们的问题必须更准确才能提供帮助
  • 我想将数组中的零值转换为Nan。

标签: python arrays numpy indexing python-xarray


【解决方案1】:

我会使用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

如果需要,它会自动移动到浮动。

【讨论】:

  • 太棒了!感谢您的回复。
猜你喜欢
  • 2018-10-31
  • 2015-09-11
  • 2019-11-12
  • 2012-12-23
  • 2022-11-22
  • 2018-08-04
  • 2019-05-31
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多