I have an array of dtype=object, where the values are either Python lists, or np.nan.
I'd like to replace the values that are np.nan with [None] (not None).
For a pure Python list, I can already do this with [ x if (x is not np.nan) else [None] for x in s ], and converting the array to a list is fine for my purpose, but out of curiosity, I wonder how this can be done with a numpy array. The difficulty is that, when using indexing, numpy tries to interpret any list as a list of values, rather than as the actual value I want to assign.
If I wanted to replace the values with 2, for example, that is easy (normal np, pd imports; as an aside, np.isnan will not work in this instance, a weakness with the choice of float NaN for generic missing values in pandas, so I use pd.isnull, as this is for an issue with pandas internals anyway):
In [53]: s
Out[53]:
array([['asdf', 'asdf'], ['asdf'], nan, ['asdf', 'asdf', 'asdf'],
['asdf', 'asdf', 'asdf']], dtype=object)
In [55]: s[pd.isnull(s)] = 2
In [56]: s
Out[56]:
array([['asdf', 'asdf'], ['asdf'], 2, ['asdf', 'asdf', 'asdf'],
['asdf', 'asdf', 'asdf']], dtype=object)
Yet trying to replace them with [None] instead replaces them with None:
In [58]: s
Out[58]:
array([['asdf', 'asdf'], ['asdf'], nan, ['asdf', 'asdf', 'asdf'],
['asdf', 'asdf', 'asdf']], dtype=object)
In [59]: s[pd.isnull(s)] = [None]
In [60]: s
Out[60]:
array([['asdf', 'asdf'], ['asdf'], None, ['asdf', 'asdf', 'asdf'],
['asdf', 'asdf', 'asdf']], dtype=object)
This is, obviously, the behavior that one wants 99% of the time. It just so happens that this time, I want to assign the list as an object. Is there any way to do so?
sitself. But that's horribly ugly; hopefully someone has a better answer…lists, you could just mutate the list in place (with[:] = …), but sadly that's not going to help here, because you obviously can't mutatenanin place into[None].