微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pandas 和 Scipy TypeError:“NoneType”和“float”的实例之间不支持“<”

如何解决Pandas 和 Scipy TypeError:“NoneType”和“float”的实例之间不支持“<”

import numpy as np 
import pandas as pd 
import math
from scipy import stats

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 score'
]

#create the data frame using pandas
hqm_dataframe = pd.DataFrame(columns = hqm_columns)
#data created using iex cloud API

#calculate the percentile scores
time_periods = [
                'One-Year','Six-Month','Three-Month','One-Month'
                ]
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] = stats.percentileofscore(hqm_dataframe[change_col],hqm_dataframe.loc[row,change_col])/100

根据最后一行代码,我收到以下错误

Traceback (most recent call last):
  File "C:\Users\Areet\Documents\cs\python\algoTrading\quantMomentum2.py",line 77,in <module>
    hqm_dataframe.loc[row,change_col])/100
  File "C:\Users\Areet\AppData\Roaming\Python\python39\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'

前面的代码行实例化并声明了错误行中正在使用的变量。从 scipy 模块中阅读“percentileofscore”后,我知道参数是一个数组和一个浮点数。我不确定为什么它不能将数据框列识别为数组。

解决方法

hqm_dataframe = hqm_dataframe.infer_objects()

上面的代码行将包含浮点值的列的数据类型设置为数据类型“float64”而不是数据类型“object”。转换后,可以使用 stats.percentileofscore 函数来比较列内的值。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。