(Python) RuntimeWarning: double_scalars """ 中遇到无效值

晚上好,

我是 Python 的初学者,每天都在学习一些新东西。这次我在执行以下代码时遇到了问题:

distance = [np.nan]
speed = [np.nan]
for i in range(1,len(data)):
    distance_dt = distance_cal(data.loc[i-1,'latitude'],data.loc[i-1,'longitude'],data.loc[i,'latitude'],data.loc[i,'longitude'])
speed_dt = distance_dt/(data.loc[i,'delta_time']/3600)
distance.append(distance_dt)
speed.append(speed_dt)
data['distance'] = np.around(distance, decimals=6)
data['speed'] = np.around(speed, decimals=6)

生成的错误是:

C:...site-packagesipykernel_launcher.py:5: RuntimeWarning: 在 double_scalars """ 中遇到无效值 C:...site-packagesipykernel_launcher.py:5: RuntimeWarning: 在 double_scalars """ 中遇到除以零

我检查过

ipykernel_launcher.py '它的代码如下'。

"""Entry point for launching an IPython kernel.

This is separate from the ipykernel package so we can avoid doing import until
after removing the cwd from sys.path.
"""

import sys

if __name__ == '__main__':
    # Remove the CWD from sys.path while we load stuff.
    # This is added back by InteractiveShellApp.init_path()
    if sys.path[0] == '':
        del sys.path[0]

    from ipykernel import kernelapp as app
    app.launch_new_instance()

此外,距离和速度是 excel 中存在的两个变量/数据集,包含值 0,1 或浮点数。

任何帮助,将不胜感激。

谢谢你。

stack overflow (Python) RuntimeWarning: invalid value encountered in double_scalars """
原文答案

答案:

作者头像

警告说:

RuntimeWarning: divide by zero encountered in double_scalars

这基本上意味着,您试图将某些东西除以 0

speed_dt = distance_dt/(data.loc[i,'delta_time']/3600)

此语句可能会导致此问题。尝试将 0 转换为大于或小于 0 的任何内容,然后再次运行。