python - Numpy string scalar comparison -


i'm seeing strange behavior numpy or i'm bad @ it.

i have structured array string field. want filter data based on field. understanding that, work:

data['somefield'][data['filterfield'] == 'someconstant'] 

but strangely, did not. i'm under assumption

data['filterfield'] == 'someconstant' 

will produce array of boolean. did not, return boolean (not array of it). instead of that, using

data['filterfield'] == full(data.shape,'someconstant',dtype=data['filterfield'].dtype) 

works expected hassle , error prone. expected behavior? if so, there shorter way create array of string?

data['filterfield'] list of strings. np.char has set of operations type of array. looks ordinary array==scalar test numeric arrays not work type of array. np.char.equal(c1,c2) compares 2 char arrays, , doesn't have provision 1 of being plain string.

so approach, generating string array, right one. can simpler (due broadcasting):

in [326]: x=np.array([(1,'one'),(2,'two'),(3,'three')],dtype='int,s10')  in [327]: x['f1'] out[327]:  array([b'one', b'two', b'three'],        dtype='|s10')  in [328]: x['f1']==np.array('two',dtype='s') out[328]: array([false,  true, false], dtype=bool) 

np.array('two') might work if default char type matches x. did testing in python3, '

there may other options using np.char functions, straightforward , simple.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -