3

consider the following:

foo = np.array(['a', 'b', 2])

bar = [x for x in foo if isinstance(x, str)]

Much to my surprise, what comes out is:

['a', 'b', '2']

So the 2 became a string. I understand that numpy is meant to deal with homogeneous arrays, but still, this is undesirable behavior, and would be nice to avoid. Suggestions?

4
  • What were you expecting or otherwise hoping to get?
    – norok2
    Commented May 25, 2020 at 17:12
  • @MarkMeyer that is (a) not very satisfying and (b) not clearly works, since pandas, like numpy, will cast everything in a column to "object"
    – Igor Rivin
    Commented May 25, 2020 at 17:12
  • @norok2 that was a joke, right? In case it was not, I was expecting ['a', 'b']
    – Igor Rivin
    Commented May 25, 2020 at 17:13
  • 1
    I am not sure if you are unhappy with the content of foo or the content of bar
    – norok2
    Commented May 25, 2020 at 17:14

2 Answers 2

2

Numpy assumes str dtype in your case. Pass the dtype explicitly, e.g.:

foo = np.array(['a', 'b', 2], dtype=object)
bar = [x for x in foo if isinstance(x, str)]
0

Im not sure whats your problem here, but I guess you want that list to only have strings/chars of letters/words?

import numpy as np
lst = ['a' ,'b', 2]
# ['a', 'b', 2]
foo = np.array([each for each in lst if str(each).isalpha()])
# array(['a', 'b'], dtype='<U1')

This ensures that only character will be in the bumpy array

2
  • My problem is, among other things, that I typed in 2 and got back '2'. You don't find that a problem?
    – Igor Rivin
    Commented May 25, 2020 at 17:27
  • 1
    You didint really specify whats your problem in the question
    – John
    Commented May 25, 2020 at 17:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.