In [23]: x = np.array(['1.2', '2.3', '1.2.3', '1.2', 'foo'])
Trying to convert the whole array to float
, results in an error if one or more strings can't be converted:
In [24]: x.astype(float)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-24-a68fda2cafea> in <module>()
----> 1 x.astype(float)
ValueError: could not convert string to float: '1.2.3'
In [25]: x[:2].astype(float)
Out[25]: array([ 1.2, 2.3])
But to find which ones can be converted, and which can't, we probably have to apply a test to each element. That requires some sort of iteration, and some sort of test.
Most of these answers have wrapped float
in a try/except
block. But look at How do I check if a string is a number (float) in Python?
for alternatives. One answer found that the float wrap was fast for valid inputs, but a regex test was faster for invalid ones (https://stackoverflow.com/a/25299619/901925).
In [30]: def isnumeric(s):
try:
float(s)
return True
except ValueError:
return False
In [31]: [isnumeric(s) for s in x]
Out[31]: [True, True, False, True, False]
In [32]: np.array([isnumeric(s) for s in x]) # for array
Out[32]: array([ True, True, False, True, False], dtype=bool)
I like list comprehension because it is common and clear (and preferred in Py3). For speed I have found that frompyfunc
has a modest advantage over other iterators (and handles multidimensional arrays):
In [34]: np.frompyfunc(isnumeric, 1,1)(x)
Out[34]: array([True, True, False, True, False], dtype=object)
In [35]: np.frompyfunc(isnumeric, 1,1)(x).astype(bool)
Out[35]: array([ True, True, False, True, False], dtype=bool)
It requires a bit more boilerplate than vectorize
, but is usually faster. But if the array or list is small, list comprehension is usually faster (avoiding numpy overhead).
======================
(edited) np.char
has a set of functions that apply string methods to the elements of an array. But the closest function is np.char.isnumeric
which just tests for numeric characters, not a full float conversion.