python - Slicing numpy recarray at "empty" rows -


i created numpy.recarray .csv-inputfile using csv2rec()-method. inputfile , consequently recarray have empty rows no data (resp. nan-values). want slice recarray @ nan-rows multiple sub-arrays, excluding nan-rows in final arrays shown below.

original recarray 2 columns:

[(1,2) (2,2) (nan,nan) (nan,nan) (4,4) (4,3)] 

2 sub-arrays without nan-values:

[(1,2) (2,2)] 

and

[(4,4) (4,3)] 

i know managed using loop maybe there's simpler , more elegant way? additionally: possible keep header-information of each column can refer columns parameter-name , not col-index after slicing?

for 2d-array:

a[~np.all(np.isnan(a),axis=1)] 

for structured array (recarray) can this:

def remove_nan(a, split=true):     cols = [i[0] in eval(str(a.dtype))]     col = cols[0]     test = ~np.isnan(a[col])     if not split:         new_len = len(a[col][test])         new = np.empty((new_len,), dtype=a.dtype)         col in cols:             new[col] = a[col][~np.isnan(a[col])]         return new     else:         indices = [i in xrange(len(a)-1) if test[i+1]!=test[i]]         return [i in np.split(a, indices) if not np.isnan(i[col][0])] 

to lines without nan use split=false. example:

a = np.array([(1,2),(2,2),(nan,nan),(nan,nan),(4,4),(4,3)], dtype=[('test',float),('col2',float)])  remove_nan(a)  #[array([(1.0, 2.0), (2.0, 2.0)], #      dtype=[('test', '<f8'), ('col2', '<f8')]), # array([(4.0, 4.0), (4.0, 3.0)], #      dtype=[('test', '<f8'), ('col2', '<f8')])] 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -