I have a for loop with a formula whit gives the following error:
RuntimeWarning: invalid value encountered in arccos
test3[i] = np.arccos(temp.x[i])
If I perform the formula withouth the np.arccos, it works. Does anyone know why the np.arccos is preventing the formula to work?
This is my simplified for loop:
for i in range(1,len(temp)):
test3[i] = np.arccos(temp.x[i])
EDIT
This is my df named temp:
time x y z bat
0 2022-05-16 12:22:58.719 0.276 0.101 -0.919 NaN
1 2022-05-16 12:23:23.719 0.264 0.085 -0.927 NaN
2 2022-05-16 12:23:48.719 0.276 0.105 -0.906 NaN
3 2022-05-16 12:24:13.719 0.260 0.101 -0.919 NaN
4 2022-05-16 12:24:38.719 0.272 0.105 -0.915 NaN
... ... ... ... ...
96376 2022-06-13 09:39:38.719 -0.083 -0.014 -0.957 NaN
96377 2022-06-13 09:40:03.719 -0.087 -0.018 -0.949 NaN
96378 2022-06-13 09:40:28.719 -0.094 -0.018 -0.962 NaN
96379 2022-06-13 09:40:53.719 -0.083 -0.018 -0.944 NaN
96380 2022-06-13 09:41:18.719 -0.551 0.006 -0.940 NaN
EDIT 2
And to make test3 variable i used:
test3 = np.zeros(len(temp))
EDIT 3 Other numpy function do seem to work, like this one:
test3[i] = np.sqrt(temp.x[i-1]**2 temp.y[i-1]**2 temp.z[i-1]**2)
EDIT 4
Now I remove my above 1 and below -1 values I get a new error:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc return self._engine.get_loc(casted_key)
File "pandas_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\hashtable_class_helper.pxi", line 2131, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas_libs\hashtable_class_helper.pxi", line 2140, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 632
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\untitled5.py", line 61, in <module>
test3[i] = np.arccos(temp.x[i])# * temp.x[i-1] temp.y[i] * temp.y[i-1] temp.z[i] * temp.z[i-1])/(np.sqrt(temp.x[i]**2 temp.y[i]**2 temp.z[i]**2)) * np.sqrt(temp.x[i-1]**2 temp.y[i-1]**2 temp.z[i-1]**2)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 942, in __getitem__
return self._get_value(key)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 1051, in _get_value
loc = self.index.get_loc(label)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: 632
CodePudding user response:
I solved the problem thanks to everyone,
I removed the above 1 and -1 values, which is just a small part of my dataset:
temp = temp.loc[(temp["x"] <= 1)
& (temp["x"] >= -1)
[(temp["y"] <= 1)
& (temp["y"] >= -1)]
[(temp["z"] <= 1)
& (temp["z"] >= -1)]]
EDIT
And because of removing some of the rows, the index becomes not 1, 2, 3, 4, 5, but 1,5,10 etc. This will break the for loop. To restore this use reindex with drop - True:
temp = temp.reset_index(drop=True)
CodePudding user response:
arccos
only takes values between -1 and 1 inclusive. If you check temp.max(axis=1)
and temp.min(axis=1)
it will tell you whether one of the values of any of the columns is outside of that range.
CodePudding user response:
Limited to the interval [-1,1]?