0

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?

3
  • Why do you want a numpy array? Why not stick with the list, where a mix of types is natural. Commented 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. Commented Aug 12, 2021 at 19:52
  • Just beware that an object dtype array is quite different from a regular numeric array. Commented Aug 12, 2021 at 20:05

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.