首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python NaN问题

Python NaN问题
EN

Stack Overflow用户
提问于 2021-08-19 18:53:19
回答 1查看 105关注 0票数 1

我正在从由熊猫数据帧生成的CSV中读取多组坐标。坐标集合的长度并不相同,所以它们被填充了NaNs。下面是我想要开始工作的代码:

代码语言:javascript
运行
复制
df=pd.read_csv('contours_20150210.csv') # reading in the dataframe and xy coordinates
c131x=np.asarray(df["contour_131_x"])
c131y=np.asarray(df["contour_131_y"])
c193x=np.asarray(df["contour_193_x"])
c193y=np.asarray(df["contour_193_y"])
c211x=np.asarray(df["contour_211_x"])
c211y=np.asarray(df["contour_211_y"])

nn_193_211=[]

dist_193_211 = distance_matrix(c193,c211) #Computing the distances between all the sets of coordinates

for i in range(len(dist_193_211[:][1])):
    nn_193_211.append([np.where(dist_193_211[i] == np.nanmin(dist_193_211[i]))[0][0],np.nanmin(dist_193_211[i])]) 
# I am looking for the nearest neighbors, both the value of the distance between them and which value that is in the list of coordinates

问题是,即使我使用的是np.nanmin,当for循环到达nans时,我也会得到以下错误。

代码语言:javascript
运行
复制
/tmp/ipykernel_3022/578260609.py:2: RuntimeWarning: All-NaN slice encountered
  nn_193_211.append([np.where(dist_193_211[i] == np.nanmin(dist_193_211[i]))[0][0],np.nanmin(dist_193_211[i])])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/tmp/ipykernel_3022/578260609.py in <module>
      1 for i in range(len(dist_193_211[:][1])):
----> 2     nn_193_211.append([np.where(dist_193_211[i] == np.nanmin(dist_193_211[i]))[0][0],np.nanmin(dist_193_211[i])])
      3 print(nn_193_211[0:100])
      4 #print(np.max(nn_193_211),np.min(nn_193_211))

IndexError: index 0 is out of bounds for axis 0 with size 0

我决定只截断填充nan(它们是数组中唯一的nan,其他地方没有丢失的数据)。因此,我阅读了Python中的nans,并运行了以下测试:

代码语言:javascript
运行
复制
print('c131x: ',c131x)
print('np.nan is np.nan:',np.nan is np.nan)
print('c131x[-1] is np.nan:',c131x[-1] is np.nan)

print(np.where(np.vectorize(c131x) is np.nan))
print(np.where(np.vectorize(c131y) is np.nan))
print(np.where(np.vectorize(c193x) is np.nan))
print(np.where(np.vectorize(c193y) is np.nan))
print(np.where(np.vectorize(c211x) is np.nan))
print(np.where(np.vectorize(c211y) is np.nan))

这是输出:

代码语言:javascript
运行
复制
c131x:  [-202.79993465 -202.49993494 -202.19993523 ...           nan           nan
           nan]
np.nan is np.nan: True
c131x[-1] is np.nan: False
(array([], dtype=int64),)
(array([], dtype=int64),)
(array([], dtype=int64),)
(array([], dtype=int64),)
(array([], dtype=int64),)
(array([], dtype=int64),)

我的理解是np.nan is np.nanc131x[-1] is np.nan都应该返回True:我是不是遗漏了什么?如果我不能确定can在哪里我就不能对数组进行切片。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-19 19:06:47

BlueBuffalo73的建议给了我一个关于不安全类型转换的错误;然而,我受到这个建议的启发并尝试了

c131x=c131x[:np.where(np.isnan(c131x))[0][0]]

这确实起作用了。我现在有了截断的坐标数组。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68853206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档