我正在 django-rest-framework 中开发一个 API,根据您的输入参数,您会得到不同的响应。API 正在计算将根据数据库返回给用户的指标。
我写了一个函数来处理 NaN 值,如下所示:
def nan_to_none(value):
if not isinstance(value, str) and value is not None and np.isnan(value):
return None
return value
这是弹出错误的响应元素:
"prog": nan_to_none(row["average_items_prog"])
这是引发问题的 SQL 行:
((((coalesce(qte_art, 0) / nullif(nb_client, 0)) - (coalesce(qte_art_n1, 0) / nullif(nb_client_n1, 0))) / (coalesce(qte_art_n1, 0) / nullif(nb_client_n1, 0))) * 100) as average_items_prog,
这是错误消息:
File "C:\Users\wdc\views.py", line 464, in get
"prog": nan_to_none(row["average_items_prog"])},
File "C:\Users\wdc\views.py", line 28, in nan_to_none
if not isinstance(value, str) and value is not None and np.isnan(value):
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1478, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我不知道如何解决这个问题!