【问题标题】:TypeError: '<' not supported between instances of 'NoneType' and 'float'TypeError:“NoneType”和“float”的实例之间不支持“<”
【发布时间】:2021-03-18 08:05:40
【问题描述】:

我正在关注 YouTube 教程,并且我根据教程编写了这段代码

import numpy as np
import pandas as pd
from scipy.stats import percentileofscore as score

my_columns = [
  'Ticker', 
  'Price', 
  'Number of Shares to Buy', 
  'One-Year Price Return',
  'One-Year Percentile Return',
  'Six-Month Price Return',
  'Six-Month Percentile Return',
  'Three-Month Price Return',
  'Three-Month Percentile Return',
  'One-Month Price Return',
  'One-Month Percentile Return'
  ]
final_df = pd.DataFrame(columns = my_columns)
# populate final_df here....
pd.set_option('display.max_columns', None)
print(final_df[:1])
time_periods = ['One-Year', 'Six-Month', 'Three-Month', 'One-Month']    
for row in final_df.index:
  for time_period in time_periods:
    change_col = f'{time_period} Price Return'
    print(type(final_df[change_col])) 
    percentile_col = f'{time_period} Percentile Return'
    print(final_df.loc[row, change_col])
    final_df.loc[row, percentile_col] = score(final_df[change_col], final_df.loc[row, change_col])
print(final_df)

它将我的数据框打印为

| Ticker |  Price  | Number of Shares to Buy | One-Year Price Return  | One-Year Percentile Return | Six-Month Price Return | Six-Month Percentile Return | Three-Month Price Return | Three-Month Percentile Return | One-Month Price Return  | One-Month Percentile Return  |
|--------|---------|-------------------------|------------------------|----------------------------|------------------------|-----------------------------|--------------------------|-------------------------------|-------------------------|------------------------------|
| A      |  120.38 | N/A                     | 0.437579               | N/A                        | 0.280969               | N/A                         | 0.198355                 | N/A                           | 0.0455988               |             N/A              |

但是当我调用 score 函数时,我得到了这个错误

<class 'pandas.core.series.Series'>
0.4320217937551543
Traceback (most recent call last):
  File "program.py", line 72, in <module>
    final_df.loc[row, percentile_col] = score(final_df[change_col], final_df.loc[row, change_col])
  File "/Users/abhisheksrivastava/Library/Python/3.7/lib/python/site-packages/scipy/stats/stats.py", line 2017, in percentileofscore
    left = np.count_nonzero(a < score)
TypeError: '<' not supported between instances of 'NoneType' and 'float'

出了什么问题?我在 YouTube 视频中看到了相同的代码。我几乎没有使用 Python 的经验

编辑:

我也试过

print(type(final_df['One-Year Price Return'])) 
print(type(final_df['Six-Month Price Return'])) 
print(type(final_df['Three-Month Price Return'])) 
print(type(final_df['One-Month Price Return'])) 
for row in final_df.index:
  final_df.loc[row, 'One-Year Percentile Return'] = score(final_df['One-Year Price Return'], final_df.loc[row, 'One-Year Price Return'])
  final_df.loc[row, 'Six-Month Percentile Return'] = score(final_df['Six-Month Price Return'], final_df.loc[row, 'Six-Month Price Return'])
  final_df.loc[row, 'Three-Month Percentile Return'] = score(final_df['Three-Month Price Return'], final_df.loc[row, 'Three-Month Price Return'])
  final_df.loc[row, 'One-Month Percentile Return'] = score(final_df['One-Month Price Return'], final_df.loc[row, 'One-Month Price Return'])
print(final_df)

但还是出现同样的错误

<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
Traceback (most recent call last):
  File "program.py", line 71, in <module>
    final_df.loc[row, 'One-Year Percentile Return'] = score(final_df['One-Year Price Return'], final_df.loc[row, 'OneYear Price Return'])
  File "/Users/abhisheksrivastava/Library/Python/3.7/lib/python/site-packages/scipy/stats/stats.py", line 2017, in percentileofscore
    left = np.count_nonzero(a < score)
TypeError: '<' not supported between instances of 'NoneType' and 'float'

【问题讨论】:

  • final_df 中的一个列名最后必须有多余的空格。因此,当您尝试使用 final_df[change_col] 访问该列时,您的数据框无法找到该列。所以它返回 None 而不是 pandas 系列。您可以输入 print(type(final_df[change_col])) 并在此处复制结果吗?它将更加清晰。
  • 我更改了代码并添加了打印语句。类型是&lt;class 'pandas.core.series.Series'&gt;
  • 请注意,您的最后一条错误消息是指“OneYear”而不是“One-Year”。我的猜测是您发布的代码并不是您正在运行的代码。
  • 它的One-Year 无处不在。抱歉,我正在尝试删除 -

标签: python pandas


【解决方案1】:

@Taras Mogetich 写的非常正确,但是您可能需要将 if 语句放在它自己的 for 循环中。 Liko so:

for row in hqm_dataframe.index:
    for time_period in time_periods:
    
        change_col = f'{time_period} Price Return'
        percentile_col = f'{time_period} Return Percentile'
        if hqm_dataframe.loc[row, change_col] == None:
            hqm_dataframe.loc[row, change_col] = 0.0

然后分别:

for row in hqm_dataframe.index:
    for time_period in time_periods:
    
        change_col = f'{time_period} Price Return'
        percentile_col = f'{time_period} Return Percentile'

        hqm_dataframe.loc[row, percentile_col] = score(hqm_dataframe[change_col], hqm_dataframe.loc[row, change_col])

【讨论】:

    【解决方案2】:

    我也在学习本教程。我深入研究了四个“___价格回报”列中的数据。查看我的批处理 API 调用,有四行的值为“None”而不是浮点数,这就是出现“NoneError”的原因,因为 percentileofscore 函数试图使用不是浮点数的“None”计算百分位数.

    为了解决这个 API 错误,我手动将计算百分位数的 None 值更改为 0,代码如下...

    time_periods = [
                    'One-Year',
                    'Six-Month',
                    'Three-Month',
                    'One-Month'
                    ]
    
    for row in hqm_dataframe.index:
        for time_period in time_periods:
            if hqm_dataframe.loc[row, f'{time_period} Price Return'] == None:
                hqm_dataframe.loc[row, f'{time_period} Price Return'] = 0
    

    【讨论】:

      【解决方案3】:

      谷歌搜索我遇到的问题很有趣,这与您正在学习的教程完全相同!

      如前所述,来自 API 调用的某些数据的值为 None,这会导致 percentileofscore 函数出错。我的解决方案是在初始创建 hqm_dataframe 时将所有 None 类型转换为整数 0。

      hqm_columns = [
          'Ticker',
          'Price',
          'Number of Shares to Buy',
          'One-Year Price Return',
          'One-Year Return Percentile',
          'Six-Month Price Return',
          'Six-Month Return Percentile',
          'Three-Month Price Return',
          'Three-Month Return Percentile',
          'One-Month Price Return',
          'One-Month Return Percentile'
      ]
      
      hqm_dataframe = pd.DataFrame(columns=hqm_columns)
      convert_none = lambda x : 0 if x is None else x
      
      for symbol_string in symbol_strings:
          batch_api_call_url = f'https://sandbox.iexapis.com/stable/stock/market/batch?symbols={symbol_string}&types=price,stats&token={IEX_CLOUD_API_TOKEN}'
          data = requests.get(batch_api_call_url).json()
          
          for symbol in symbol_string.split(','):
              hqm_dataframe = hqm_dataframe.append(
                  pd.Series(
                      [
                          symbol,
                          data[symbol]['price'],
                          'N/A',
                          convert_none(data[symbol]['stats']['year1ChangePercent']),
                          'N/A',
                          convert_none(data[symbol]['stats']['month6ChangePercent']),
                          'N/A',
                          convert_none(data[symbol]['stats']['month3ChangePercent']),
                          'N/A',
                          convert_none(data[symbol]['stats']['month1ChangePercent']),
                          'N/A'
                      ],
                      index = hqm_columns
                  ),
                  ignore_index=True
              )
      

      【讨论】:

        【解决方案4】:

        如下所示,只需将 None 值替换为 0,

        hqm_dataframe.fillna(0,inplace=True)
        

        【讨论】:

          【解决方案5】:

          填充final_df后,也可以这样做:

          final_df.fillna(value=0, inplace=True)
          

          如果您只想将每个 NaN 替换为 0。

          【讨论】:

            【解决方案6】:

            您确定这是整个代码吗?在我的情况下,它返回空数据框。请提供更多详细信息

            【讨论】:

            • 对 Web 服务的调用很长,并没有真正增加多少价值。我已经提供了返回到上面数据框中的数据。
            • 为了给您合适的答案,了解 DataFrame 内部的内容很重要。问题在于得分函数。我需要知道哪些值是 None 以及何时
            【解决方案7】:

            大多数其他回复都是正确的,问题是数据框中没有值,并且 scipy stats 的 percentileofscore 方法不知道如何解析这些值。我有一个不同的解决方案,它不涉及解析数据帧上的每个条目。

            我使用数据框的 .replace 方法将所有 None 条目替换为 0。inplace = True 在那里,以便将更改保存到数据框,而不必分配它。

            hqm_dataframe.replace([None], 0, inplace = True)
            

            【讨论】:

              【解决方案8】:

              使用 np.nan 代替 'N/A' 并将浮点类型设置为列。

              final_df = pd.DataFrame(columns = my_columns)
              
              for symbol_string in symbol_strings:
                  batch_api_call_url = f'https://sandbox.iexapis.com/stable/stock/market/batch?symbols={symbol_string}&types=price,stats&token={IEX_CLOUD_API_TOKEN}'
                  data = requests.get(batch_api_call_url).json()
              #    print(symbol_string.split(','))
              #    print(data['AAPL']['stats'])
                  for symbol in symbol_string.split(','):
                      final_df = final_df.append(
                          pd.Series(
                              [
                                  symbol,
                                  data[symbol]['price'],
                                  data[symbol]['stats']['year1ChangePercent'],
                                  np.nan
                              ],
                              index = my_columns
                          ),
                          ignore_index=True
                      )
              

              hqm_df = pd.DataFrame(columns = hqm_columns)
              
              for symbol_string in symbol_strings:
                  batch_api_call_url = f'https://sandbox.iexapis.com/stable/stock/market/batch?symbols={symbol_string}&types=price,stats&token={IEX_CLOUD_API_TOKEN}'
                  data = requests.get(batch_api_call_url).json()
                  for symbol in symbol_string.split(','):
                      hqm_df = hqm_df.append(
                          pd.Series(
                              [
                                  symbol,
                                  data[symbol]['price'],
                                  np.nan,
                                  data[symbol]['stats']['year1ChangePercent'],
                                  np.nan,
                                  data[symbol]['stats']['month6ChangePercent'],
                                  np.nan,
                                  data[symbol]['stats']['month3ChangePercent'],
                                  np.nan,
                                  data[symbol]['stats']['month1ChangePercent'],
                                  np.nan
                              ],
                              index = hqm_columns
                          ),
                          ignore_index=True
                      )
              

              hqm_df['One-Year Price Return'] = hqm_df['One-Year Price Return'].astype('float')
              hqm_df['Six-Month Price Return'] = hqm_df['Six-Month Price Return'].astype('float')
              hqm_df['Three-Month Price Return'] = hqm_df['Three-Month Price Return'].astype('float')
              hqm_df['One-Month Price Return'] = hqm_df['One-Month Price Return'].astype('float')
              

              【讨论】:

                猜你喜欢
                • 2020-05-05
                • 1970-01-01
                • 2021-05-20
                • 1970-01-01
                • 2020-02-12
                • 1970-01-01
                • 2018-01-10
                • 2018-11-22
                • 2020-06-02
                相关资源
                最近更新 更多