standard
standard

Reputation: 25

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. Why am I still receiving this error even after filtering out NoneTypes with the first line of code above?

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).

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).

I believe the problem is that not all equities have the 'marketcap' data field populated, so iterating over them returns the 'NoneType' error message. I'm typing this off of memory so do not have the exact error message handy but will update later.

Upvotes: 0

Views: 170

Answers (1)

MaMaG
MaMaG

Reputation: 379

I'm going to guess that data is some sort of iterable (list, dataframe, matrix). If that's true, then the first conditional is comparing data to None. Any iterable, even one that contains only None's is not None. So the first conditional is true.

But the 2nd conditional (if data is, say, a numpy array) is elementwise. So I'm guessing that one or more of the elements in the numpy array is None and that's why it fails.

If this is really a numpy array, then do it the numpy way:

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

Upvotes: 1

Related Questions