python - TypeError:“NoneType”和“float”的实例之间不支持“<”
问题描述
我正在关注 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'
解决方案
@Taras Mogetich 写的非常正确,但是您可能需要将 if 语句放在它自己的 for 循环中。丽可这样:
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])
推荐阅读
- swiftui - SwiftUI ContextMenu 导航到另一个视图
- python - 无法在 Django 中显示 {% block content %}{% endblock %} 的内容
- azure-data-factory - Azure DataLake Analytics U-SQL 管道活动错误
- python - 用python对excel文件进行简单操作
- python - 如何证明python标签中的文本?
- python - 关于 numpy 数组操作的内存错误
- java - Thymeleaf 条件图像插入条件下
- azure - 在 azure 上部署本地 elasticsearch 集群
- java - Hibernate 二级缓存总是在获取所有实体时调用数据库
- firefox - 原生 WebRTC SDK 不接受 Firefox 生成的报价