1

I am trying to get the elements in an ndarray that are strings. That is, exclude the elements that are integers and floats.

Lets say I have this array:

x = np.array([1,'hello',2,'world'])

I want it to return:

array(['hello','world'],dtype = object)

I've tried doing np.where(x == np.str_) to get the indices where that condition is true, but it's not working.

Any help is much appreciated.

3
  • 1
    If you check x after your first line, you'll notice the entire array has dtype='<U11' therefore there is nothing to filter Commented Jul 30, 2021 at 19:02
  • 1
    See this post that discusses heterogeneous data in numpy. Commented Jul 30, 2021 at 19:03
  • Run type(x[0]) and you will see that the integers are actually being stored as numpy.str. Commented Jul 30, 2021 at 19:06

2 Answers 2

1

You can make a function to do it, and loop over the array:

def getridofnumbers(num):
    try:
        x = int(num)
    except:
        return True
    return False

output = np.array([i for i in x if getridofnumbers(i)])

if we want to keep all the numpy goodness (broadcasting etc), we can convert that into a ufunc using vectorize (or np.frompyfunc):

import numpy as np
#vectorize the fucntion, with a boolean return type
getrid = np.vectorize(getridofnumbers, otypes=[bool])

x[getrid(x)]
array(['hello', 'world'], dtype='<U11')

#or ufunc, which will require casting:
getrid = np.frompyfunc(getridofnumbers, 1, 1)
x[getrid(x).astype(bool)]
Sign up to request clarification or add additional context in comments.

Comments

0

When you run x = np.array([1,'hello',2,'world']), numpy converts everything to string type.

If it is one dimensional array, you can use:

y = np.array([i for i in x if not i.replace(".","",1).replace("e+","").replace("e-","").replace("-","").isnumeric()])

to get all non-numeric values.

It can identify all floats with negative sign and and e+/e- )

like, for input: x = np.array([1,'hello',+2e-50,'world', 2e+50,-2, 3/4, 6.5 , "!"]) output will be : array(['hello', 'world', '!'], dtype='<U5')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.