Python – (Python) RuntimeWarning: invalid value encountered in double_scalars “””

python

Good Evening,

I am beginner in Python and learning some new things daily.
This time i am incurring a problem while executing the following code:

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)

Generated Error is:

C:…site-packages\ipykernel_launcher.py:5: RuntimeWarning: invalid value encountered in double_scalars
"""
C:…site-packages\ipykernel_launcher.py:5: RuntimeWarning: divide by zero encountered in double_scalars
"""

I have checked

\ipykernel_launcher.py 'It's code is given below'.

"""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()

Moreover, distance and speed are two variables/datsets present in excel and contain value either 0,1 or floating number.

Any help would be appreciated.

Thank you.

Best Answer

The warning says:

RuntimeWarning: divide by zero encountered in double_scalars

Which basically means, you are trying to divide something by 0.

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

This statement might be causing this issue. Try and convert 0 into a anything more or less than 0 and then run again.