In my code, I must go from a large nested list (python list of python lists) and in each sublist, some entries may be the numpy values NaN. When I create this nested list it looks something like this:
import numpy as np
nested_list = [[np.nan, 19], ['a', np.nan]]
>>> print(nested_list)
[[nan, 19], ['a', nan]]
I expect when I evaluate the equality of a NaN element of the sublist against itself, I should get False due to it being a null value which I do:
>>>print(nested_list[1][1] == nested_list[1][1])
False
Now, I want to turn this nested list into a 2d numpy array, but when I do, the NaN values just turn into strings and do not retain their null-ness:
arr = np.array(list_of_lists)
>>>print(arr)
[['nan' '19']
['a' 'nan']]
>>>print(arr[1][1] == arr[1][1])
True
How do I keep NaN from being turned into a string?
-
Why do you want a numpy array? Why not stick with the list, where a mix of types is natural.hpaulj– hpaulj2021年08月12日 19:44:17 +00:00Commented Aug 12, 2021 at 19:44
-
@hpaulj I had some previous code written when the object I'm dealing with is a numpy array from the start, but the code I'm writing now necessitates me starting with a list, so I wanted to convert back to what the code already knew how to deal with.cameronpoe– cameronpoe2021年08月12日 19:52:39 +00:00Commented Aug 12, 2021 at 19:52
-
Just beware that an object dtype array is quite different from a regular numeric array.hpaulj– hpaulj2021年08月12日 20:05:59 +00:00Commented Aug 12, 2021 at 20:05
1 Answer 1
You can do it like this
import numpy as np
nested_list = [[np.nan, 19], ['a', np.nan]]
np.array(nested_list, dtype='object')