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, percentile_col] = stats.percentileofscore(hqm_dataframe[change_col], 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.expert_objects()
上面的代码行将包含浮点值的列的数据类型设置为数据类型“float64”,而不是数据类型“object”。转换后,可以使用stats.percentileofscore函数比较列中的值
相关问题 更多 >
编程相关推荐