Forced conversion of non-numeric numpy arrays with NAN replacement

你说的曾经没有我的故事 提交于 2019-11-27 15:11:55

You can convert an array of strings into an array of floats (with NaNs) using np.genfromtxt:

In [83]: np.set_printoptions(precision=3, suppress=True)

In [84]: np.genfromtxt(np.array(['1','2','3.14','1e-3','b','nan','inf','-inf']))
Out[84]: array([ 1.   ,  2.   ,  3.14 ,  0.001,    nan,    nan,    inf,   -inf])

Here is a way to identify "numeric" strings:

In [34]: x
Out[34]: 
array(['1', '2', 'a'], 
      dtype='|S1')

In [35]: x.astype('unicode')
Out[35]: 
array([u'1', u'2', u'a'], 
      dtype='<U1')

In [36]: np.char.isnumeric(x.astype('unicode'))
Out[36]: array([ True,  True, False], dtype=bool)

Note that "numeric" means a unicode that contains only digit characters -- that is, characters that have the Unicode numeric value property. It does not include the decimal point. So u'1.3' is not considered "numeric".

Bill

If you happen to be using pandas as well you could use the pd.to_numeric() method:

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: x = np.array(['1', '2', 'a'])

In [4]: pd.to_numeric(x, errors='coerce')
Out[4]: array([  1.,   2.,  nan])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!