首页 > 解决方案 > np.astype 不改变数组类型

问题描述

我正在摄取许多数据集,其中包括纬度和经度值。其中之一的 lat 和 lon 字段设置为 float64,而其他所有人都使用 float32。所以,我最初试图简单地转换类型 - 除了类型转换似乎没有发生。

更糟糕的是,我没有收到任何错误消息,因此很难排除故障。

我这里有一些明显的错误吗?lat_ref 应该存在于一个数组中,最初是一个 Float64。我想用一个值为 Float32 的数组替换该数组,我不介意这些值是否被截断以匹配 float32,因为带有 float64 的纬度将以纳米为单位测量。

示例代码:

lat_ref = nc.variables['lat'][:] #This is where I read in the lat from my data
lat_ref = lat_ref.astype(np.float32)

print("The shape of lat_ref is: " + str(np.shape(lat_ref)))
print("The dtype of lat_ref is: " + str(lat_ref.dtype))

打印的结果是:

The shape of lat_ref is: (681,)
The dtype of lat_ref is: float64

我的其他数据集响应打印(没有 .astype 转换),如下所示:

The shape of lat_ref is: (408,)
The dtype of lat_ref is: float32

我希望转换使 lat_ref 像上面那样。

标签: pythonpython-3.xnumpynetcdf

解决方案


推荐阅读