Since it's an object array, we might as well iterate and use plain python split:
In [118]: a = np.array([['1,2,3'], ['3,4,5']], dtype=object)
In [119]: a.shape
Out[119]: (2, 1)
In [120]: np.array([x.split(',') for x in a.ravel()])
Out[120]:
array([['1', '2', '3'],
['3', '4', '5']], dtype='<U1')
In [122]: np.array([x.split(',') for x in a.ravel()],dtype=float)
Out[122]:
array([[1., 2., 3.],
[3., 4., 5.]])
I raveled it to simplify iteration. Plus the result doesn't need that 2nd size 1 dimension.
There is a np.char
function that applies split
to elements of an array, but the result is messier:
In [129]: a.astype(str)
Out[129]:
array([['1,2,3'],
['3,4,5']], dtype='<U5')
In [130]: np.char.split(_, sep=',')
Out[130]:
array([[list(['1', '2', '3'])],
[list(['3', '4', '5'])]], dtype=object)
In [138]: np.stack(Out[130].ravel()).astype(float)
Out[138]:
array([[1., 2., 3.],
[3., 4., 5.]])
Another way:
In [132]: f = np.frompyfunc(lambda astr: np.array(astr.split(','),float),1,1)
In [133]: f(a)
Out[133]:
array([[array([1., 2., 3.])],
[array([3., 4., 5.])]], dtype=object)
In [136]: np.stack(_.ravel())
Out[136]:
array([[1., 2., 3.],
[3., 4., 5.]])