Quick and dirty solution:
from numpy import nan, array
a = '[15, 8.0, 5.0, 5.0, nan, nan, nan, nan, nan, nan]'
arr = array(eval(a))
This brings nan into the namescape and then evaluates a as a Python expression. The result is a list that can be readily converted into a numpy array.
Be aware that using eval is risky if your strings come from an untrusted source (such as user input), since it may lead to arbitrary code execution. A safer version would be:
import ast
import numpy as np
def convert(s):
# replace the nan values so that ast.literal_eval() can interpret them
s = s.replace('nan', 'None')
# safely evaluate s as Python expression
l = ast.literal_eval(s)
# replace the Nones back to np.nans
l = [x if x is not None else np.nan for x in l]
return np.array(l)
Then:
a = '[15, 8.0, 5.0, 5.0, nan, nan, nan, nan, nan, nan]'
convert(a)
returns
array([15., 8., 5., 5., nan, nan, nan, nan, nan, nan])