I have a sparse array of shape (863, 923, 2) that contains lot of NANs:
[[[ 43.06010628 -11.01121568]
[ 25.03068277 16.3949826 ]
[-23.75853158 -10.95350074]
...
[ 25.52110353 3.00428452]
[ 32.66945663 9.76115107]
[ 19.1341548 8.48547008]]
[[ 19.08099208 11.27167832]
[-29.4360534 -12.39131814]
[ 11.24612069 14.38915742]
...
[ 16.6897315 10.04601296]
[ 30.09409518 17.09382562]
[ -9.47312129 -9.57484782]]
[[ 21.22006655 -5.01340343]
[ 11.65512749 2.32398374]
[-22.14668148 -11.05883399]
...
[ nan nan]
[ nan nan]
[ nan nan]]
...
[[ 32.32522443 -3.73563526]
[ 30.88408144 -2.92184744]
[ 37.44548043 -21.8209554 ]
...
[ nan nan]
[ nan nan]
[ nan nan]]
[[ 36.85471348 -7.86696711]
[ 37.20204074 -6.32105844]
[ 32.32522443 -3.73563526]
...
[ nan nan]
[ nan nan]
[ nan nan]]
[[ 34.21397091 -5.88930588]
[ 35.88819735 -7.64992589]
[ 35.48958094 -10.34708285]
...
[ nan nan]
[ nan nan]
[ nan nan]]]
I would like to remove all nan-containing subarrays while preserving the dimensionality of the array. My understanding is that the shape of the array will change to something like (m, n, 2) but am unable to produce such an array after removing NANs.
Here is my attempt:
nonnanarr = arr[~np.isnan(arr).any(axis=-1)].reshape((863, -1, 2))
And here is the error message:
Traceback (most recent call last):
File "c:\Users\username\Desktop\observables\my_script.py", line 167, in <module>
main()
File "c:\Users\username\Desktop\observables\my_script.py", line 104, in main
time_stamp_num, agents_num, spatial_dimensions_num = dataframe_splitter()
File "c:\Users\username\Desktop\observables\utilities.py", line 1351, in dataframe_splitter
nonnan_arr = arr[~np.isnan(arr).any(axis=-1)].reshape(
ValueError: cannot reshape array of size 226512 into shape (863,newaxis,2)