- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 csv 转换为 numpy 数组。在 numpy 数组中,我用 NaN 替换了一些元素。然后,我想找到 numpy 数组中 NaN 元素的索引。代码是:
import pandas as pd
import matplotlib.pyplot as plyt
import numpy as np
filename = 'wether.csv'
df = pd.read_csv(filename,header = None )
list = df.values.tolist()
labels = list[0]
wether_list = list[1:]
year = []
month = []
day = []
max_temp = []
for i in wether_list:
year.append(i[1])
month.append(i[2])
day.append(i[3])
max_temp.append(i[5])
mid = len(max_temp) // 2
temps = np.array(max_temp[mid:])
temps[np.where(np.array(temps) == -99.9)] = np.nan
plyt.plot(temps,marker = '.',color = 'black',linestyle = 'none')
# plyt.show()
print(np.where(np.isnan(temps))[0])
# print(len(pd.isnull(np.array(temps))))
当我执行此操作时,我收到警告和错误。警告是:
wether.py:26: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
temps[np.where(np.array(temps) == -99.9)] = np.nan
错误是:
Traceback (most recent call last):
File "wether.py", line 30, in <module>
print(np.where(np.isnan(temps))[0])
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
这是我正在使用的数据集的一部分:
83168,2014,9,7,0.00000,89.00000,78.00000, 83.50000
83168,2014,9,22,1.62000,90.00000,72.00000, 81.00000
83168,2014,9,23,0.50000,87.00000,74.00000, 80.50000
83168,2014,9,24,0.35000,82.00000,73.00000, 77.50000
83168,2014,9,25,0.60000,85.00000,75.00000, 80.00000
83168,2014,9,26,0.76000,89.00000,77.00000, 83.00000
83168,2014,9,27,0.00000,89.00000,79.00000, 84.00000
83168,2014,9,28,0.00000,90.00000,81.00000, 85.50000
83168,2014,9,29,0.00000,90.00000,79.00000, 84.50000
83168,2014,9,30,0.50000,89.00000,75.00000, 82.00000
83168,2014,10,1,0.02000,91.00000,75.00000, 83.00000
83168,2014,10,2,0.03000,93.00000,77.00000, 85.00000
83168,2014,10,3,1.40000,93.00000,75.00000, 84.00000
83168,2014,10,4,0.06000,89.00000,75.00000, 82.00000
83168,2014,10,5,0.22000,91.00000,68.00000, 79.50000
83168,2014,10,6,0.00000,84.00000,68.00000, 76.00000
83168,2014,10,7,0.17000,85.00000,73.00000, 79.00000
83168,2014,10,8,0.06000,84.00000,73.00000, 78.50000
83168,2014,10,9,0.00000,87.00000,73.00000, 80.00000
83168,2014,10,10,0.00000,88.00000,80.00000, 84.00000
83168,2014,10,11,0.00000,87.00000,80.00000, 83.50000
83168,2014,10,12,0.00000,88.00000,80.00000, 84.00000
83168,2014,10,13,0.00000,88.00000,81.00000, 84.50000
83168,2014,10,14,0.04000,88.00000,77.00000, 82.50000
83168,2014,10,15,0.00000,88.00000,77.00000, 82.50000
83168,2014,10,16,0.09000,89.00000,72.00000, 80.50000
83168,2014,10,17,0.00000,85.00000,67.00000, 76.00000
83168,2014,10,18,0.00000,84.00000,65.00000, 74.50000
83168,2014,10,19,0.00000,84.00000,65.00000, 74.50000
83168,2014,10,20,0.00000,85.00000,69.00000, 77.00000
83168,2014,10,21,0.77000,87.00000,76.00000, 81.50000
83168,2014,10,22,0.69000,81.00000,71.00000, 76.00000
83168,2014,10,23,0.31000,82.00000,72.00000, 77.00000
83168,2014,10,24,0.71000,79.00000,73.00000, 76.00000
83168,2014,10,25,0.00000,81.00000,68.00000, 74.50000
83168,2014,10,26,0.00000,82.00000,67.00000, 74.50000
83168,2014,10,27,0.00000,83.00000,64.00000, 73.50000
83168,2014,10,28,0.00000,83.00000,66.00000, 74.50000
83168,2014,10,29,0.03000,86.00000,76.00000, 81.00000
83168,2014,10,30,0.00000,85.00000,69.00000, 77.00000
83168,2014,10,31,0.00000,85.00000,69.00000, 77.00000
83168,2014,11,1,0.00000,86.00000,59.00000, 72.50000
83168,2014,11,2,0.00000,77.00000,52.00000, 64.50000
83168,2014,11,3,0.00000,70.00000,52.00000, 61.00000
83168,2014,11,4,0.00000,77.00000,59.00000, 68.00000
83168,2014,11,5,0.02000,79.00000,73.00000, 76.00000
83168,2014,11,6,0.02000,82.00000,75.00000, 78.50000
83168,2014,11,7,0.00000,83.00000,66.00000, 74.50000
83168,2014,11,8,0.00000,84.00000,65.00000, 74.50000
83168,2014,11,9,0.00000,84.00000,65.00000, 74.50000
83168,2014,11,10,1.20000,72.00000,65.00000, 68.50000
83168,2014,11,11,0.08000,77.00000,61.00000, 69.00000
83168,2014,11,12,0.00000,80.00000,61.00000, 70.50000
83168,2014,11,13,0.00000,83.00000,63.00000, 73.00000
83168,2014,11,14,0.00000,83.00000,65.00000, 74.00000
83168,2014,11,15,0.00000,82.00000,64.00000, 73.00000
83168,2014,11,16,0.00000,83.00000,64.00000, 73.50000
83168,2014,11,17,0.07000,84.00000,64.00000, 74.00000
83168,2014,11,18,0.00000,86.00000,71.00000, 78.50000
83168,2014,11,19,0.57000,78.00000,55.00000, 66.50000
83168,2014,11,20,0.05000,72.00000,56.00000, 64.00000
83168,2014,11,21,0.05000,77.00000,63.00000, 70.00000
83168,2014,11,22,0.22000,77.00000,69.00000, 73.00000
83168,2014,11,23,0.06000,79.00000,76.00000, 77.50000
83168,2014,11,24,0.02000,84.00000,78.00000, 81.00000
83168,2014,11,25,0.00000,86.00000,78.00000, 82.00000
83168,2014,11,26,0.07000,85.00000,77.00000, 81.00000
83168,2014,11,27,0.21000,82.00000,55.00000, 68.50000
83168,2014,11,28,0.00000,73.00000,53.00000, 63.00000
83168,2015,1,8,0.00000,80.00000,57.00000,
83168,2015,1,9,0.05000,72.00000,56.00000,
83168,2015,1,10,0.00000,72.00000,57.00000,
83168,2015,1,11,0.00000,80.00000,57.00000,
83168,2015,1,12,0.05000,80.00000,59.00000,
83168,2015,1,13,0.85000,81.00000,69.00000,
83168,2015,1,14,0.05000,81.00000,68.00000,
83168,2015,1,15,0.00000,81.00000,64.00000,
83168,2015,1,16,0.00000,78.00000,63.00000,
83168,2015,1,17,0.00000,73.00000,55.00000,
83168,2015,1,18,0.00000,76.00000,55.00000,
83168,2015,1,19,0.00000,78.00000,55.00000,
83168,2015,1,20,0.00000,75.00000,56.00000,
83168,2015,1,21,0.02000,73.00000,65.00000,
83168,2015,1,22,0.00000,80.00000,64.00000,
83168,2015,1,23,0.00000,80.00000,71.00000,
83168,2015,1,24,0.00000,79.00000,72.00000,
83168,2015,1,25,0.00000,79.00000,49.00000,
83168,2015,1,26,0.00000,79.00000,49.00000,
83168,2015,1,27,0.10000,75.00000,53.00000,
83168,2015,1,28,0.00000,68.00000,53.00000,
83168,2015,1,29,0.00000,69.00000,53.00000,
83168,2015,1,30,0.00000,72.00000,60.00000,
83168,2015,1,31,0.00000,76.00000,58.00000,
83168,2015,2,1,0.00000,76.00000,58.00000,
83168,2015,2,2,0.05000,77.00000,58.00000,
83168,2015,2,3,0.00000,84.00000,56.00000,
83168,2015,2,4,0.00000,76.00000,56.00000,
我无法纠正错误。如何克服第26行的警告?如何解决这个错误?
更新:当我以不同的方式尝试相同的事情(例如从文件读取数据集而不是转换为数据帧)时,我没有收到错误。原因是什么?代码是:
weather_filename = 'wether.csv'
weather_file = open(weather_filename)
weather_data = weather_file.read()
weather_file.close()
# Break the weather records into lines
lines = weather_data.split('\n')
labels = lines[0]
values = lines[1:]
n_values = len(values)
# Break the list of comma-separated value strings
# into lists of values.
year = []
month = []
day = []
max_temp = []
j_year = 1
j_month = 2
j_day = 3
j_max_temp = 5
for i_row in range(n_values):
split_values = values[i_row].split(',')
if len(split_values) >= j_max_temp:
year.append(int(split_values[j_year]))
month.append(int(split_values[j_month]))
day.append(int(split_values[j_day]))
max_temp.append(float(split_values[j_max_temp]))
# Isolate the recent data.
i_mid = len(max_temp) // 2
temps = np.array(max_temp[i_mid:])
year = year[i_mid:]
month = month[i_mid:]
day = day[i_mid:]
temps[np.where(temps == -99.9)] = np.nan
# Remove all the nans.
# Trim both ends and fill nans in the middle.
# Find the first non-nan.
i_start = np.where(np.logical_not(np.isnan(temps)))[0][0]
temps = temps[i_start:]
year = year[i_start:]
month = month[i_start:]
day = day[i_start:]
i_nans = np.where(np.isnan(temps))[0]
print(i_nans)
第一个代码有什么问题,为什么第二个代码甚至没有发出警告?
最佳答案
发布它可能会对 future 的用户有所帮助。
正如其他人正确指出的那样,np.isnan 不适用于 object
或 string
dtypes。如果您使用 pandas,如上所述 here您可以直接使用 pd.isnull
,它应该适用于您的情况。
import pandas as pd
import numpy as np
var1 = ''
var2 = np.nan
>>> type(var1)
<class 'str'>
>>> type(var2)
<class 'float'>
>>> pd.isnull(var1)
False
>>> pd.isnull(var2)
True
关于python - TypeError : ufunc 'isnan' not supported for the input types, 并且无法安全强制输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52657223/
Soooooo isNaN 在 JavaScript 中显然是错误的,例如: isNaN('') isNaN(' ') isNaN(true) isNaN(false) isNaN([0]) 返回
这个问题在这里已经有了答案: Why does isNaN(" ") (string with spaces) equal false? (25 个答案) Why does Number.isNaN
我对 NaN 的工作原理感到困惑。我执行了 isNaN(undefined) 它返回了 true 。但是,如果我将使用 Number.isNaN(undefined),它将返回 false。那么我应该
我对 NaN 的工作原理感到困惑。我执行了 isNaN(undefined) 它返回了 true 。但是,如果我将使用 Number.isNaN(undefined),它将返回 false。那么我应该
我需要一个内置函数来检查变量是否包含 Javascript 中的有效数字,如下 this link我尝试使用 is isNaN ,但是当我使用一个引号 ('') 或两个引号 ("") 时,结果始终为
据我所知,== 检查值是否相等,而 is 检查值背后结构的同一性(比如 == = 在某些其他语言中)。 鉴于此,我不明白以下内容: np.isnan(30) == False Out[19]: Tru
在 java 中,我们可以对 float 和 double 值使用 isNan() 方法。 例如: if (!Double.isNaN(0.01)) { // condition happ
我正在尝试开发一个小型财务页面,您可以在其中输入您的元素的价格、您可以负担的预付款以及您想要支付的年数,当您提交时,它会告诉您您的金额我们将按月付款。 我遇到的问题是,如果有人在其中一个字段中输入字母
我有一个脚本,我可以在一个文本框中输入一个数字,它将计算其他文本框中的等效值。 $("input[type=text]").keyup(function () { var number
根据MDN page 1 关于isNaN(强调我的): Unlike all other possible values in JavaScript, it is not possible to re
我无法在我的Visual C++项目中编译glm::isnan()。 #include glm::vec3 my_vector = ... ; bool b = glm::isnan(my_vect
我正在创建一个小计算器。其中部分功能允许您输入最多 4 个数字,但也可以只输入 1,2 或 3 个数字。我的代码可以正常工作,但当我只输入 1、2 或 3 个数字时,我会得到未输入数字的 NaN 结果
我有两个字段。第一个是名称字段,另一个是数字字段。我想检查数字字段是否为数字,如果不是,则将使用 select() 并且背景变为黄色而不是蓝色。不知何故,我没有得到我想要的。 window.onloa
新手在这里学习 我正在尝试检查输入到我的表单中的数据,如果输入到第二个或第三个输入中的数据不是数字(即按字母顺序排列),则向用户显示信息不是数字的通知。我正在使用“isNaN”函数来执行此操作,但根据
我正在使用 isNan 函数来检查输入的值是否为数字: if(isNaN(num)) { alert('I am not a number'); } else { // } 但是,如
这是我放在文本框上以限制输入数字的属性。 onKeyPress="if(isNaN(String.fromCharCode(event.keyCode))) event.preventDefault
我有一个函数来测试提示输入是否是数字,如下所示: function myFunction() { var person = prompt("Please enter your name", "
我是 JavaScript 的新手,我尝试运行一个简单的脚本,如果它没有正确完成(即如果条目不是数字),它会给我的字段一个红色的边框,但如果该字段则将边框颜色变为绿色然后正确填写,但无法提交表单,因为
只是 try catch 非数字输入阅读很多内容。尝试了decimalDigitCharacterSet(发现很难相信以“decimal”一词开头的东西不包含小数)。尝试可变字符集来添加小数。一直在努
isnan() 函数也接受 .(点)。如何预防它们。这是示例代码: var Price = $("#Price").val(); if (Pric
我是一名优秀的程序员,十分优秀!