简体   繁体   English

为什么我在应该解决时仍然收到“NoneType”错误?

[英]Why am I still receiving a 'NoneType' error when it should be resolved?

I am calling data where some cells might be blank and attempting to filter it based on certain criteria.我正在调用某些单元格可能为空白的数据并尝试根据某些条件对其进行过滤。

if data is not None:
   if data <= 50000:
     print(data)

I am getting an error that states that 'NoneType' data is not comparable to an integer.我收到一条错误消息,指出“NoneType”数据无法与整数进行比较。 Why am I still receiving this error even after filtering out NoneTypes with the first line of code above?为什么即使在使用上面的第一行代码过滤掉 NoneTypes 后,我仍然收到此错误?

Edit for clarification: The code is actually set up like this, which pulls data from the Polygon api data feed ( https://polygon.io/docs/#getting-started ).编辑澄清:代码实际上是这样设置的,它从 Polygon api 数据源 ( https://polygon.io/docs/#getting-started ) 中提取数据。

tickers = api.polygon.all_tickers()
for ticker in tickers:
   if ticker.prevDay['c'] >= 20:
     company = api.polygon.company(ticker.ticker)
     if company.marketcap is not None:
        if company.marketcap <= 500000000:
           print(ticker.ticker)

This will grab all available equities from the data feed, which have certain attributes like "ticker" (returns symbol) or "prevDay" (returns previous day open, high, low, or close).这将从数据馈送中获取所有可用股票,这些股票具有某些属性,例如“股票代码”(返回符号)或“prevDay”(返回前一天的开盘价、最高价、最低价或收盘价)。

I believe the problem is that not all equities have the 'marketcap' data field populated, so iterating over them returns the 'NoneType' error message.我认为问题在于并非所有股票都填充了“市值”数据字段,因此对它们进行迭代会返回“NoneType”错误消息。 I'm typing this off of memory so do not have the exact error message handy but will update later.我正在从内存中键入此内容,因此手边没有确切的错误消息,但稍后会更新。

I'm going to guess that data is some sort of iterable (list, dataframe, matrix).我会猜测data是某种可迭代的(列表、数据框、矩阵)。 If that's true, then the first conditional is comparing data to None.如果这是真的,那么第一个条件是将数据与 None 进行比较。 Any iterable, even one that contains only None's is not None.任何可迭代的,即使是只包含 None 的也不是 None。 So the first conditional is true.所以第一个条件为真。

But the 2nd conditional (if data is, say, a numpy array) is elementwise.但是第二个条件(如果data是一个 numpy 数组)是元素方面的。 So I'm guessing that one or more of the elements in the numpy array is None and that's why it fails.所以我猜测 numpy 数组中的一个或多个元素是 None ,这就是它失败的原因。

If this is really a numpy array, then do it the numpy way:如果这真的是一个 numpy 数组,那么按照 numpy 的方式进行:

data_nonnan = data[np.where(~np.isnan(data))]
print(data_nonnan[data_nonnan < 50000])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我不迭代时,为什么会出现“&#39;NoneType&#39;是不可迭代的”错误? - Why a “'NoneType' is not iterable” error when I am not iterating? 为什么我会收到此错误“无法解压缩不可迭代的 NoneType 对象”? - Why am I receiving this error "cannot un-pack non-iterable NoneType object"? 为什么在不是“ NoneType”的数据上出现“ NoneType”类型错误? - Why am I getting a 'NoneType' type error on data that is not a 'NoneType'? 为什么我在循环pandas数据帧时收到此错误 - Why am I receiving this error when looping through pandas dataframe 为什么在 Anaconda 中安装软件包或 JupyterLab 时收到错误消息? - Why am I receiving an error when installing packages or JupyterLab in Anaconda? 为什么在尝试比较两个列表时出现错误? - Why am I receiving an error when trying to compare two lists? 为什么我收到错误错误:当我使用 Pandas 时,模块 'string' 没有属性 'lower'? - Why I am receiving the error Error: module ‘string’ has no attribute ‘lower’ when I am using Pandas? 为什么我收到此错误:_mysql_exceptions.OperationalError:(1241,&#39;Operand应该包含1列&#39;) - Why am I receiving this error: _mysql_exceptions.OperationalError: (1241, 'Operand should contain 1 column(s)') 为什么我会收到多处理的递归错误? - Why am I receiving a Recursion error with multiprocessing? 为什么我收到价值错误 - Why am I receiving a value error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM