Removing nan from an array containing strings in Python

Viewed 30

I want to remove nan from array A containing strings. I tried using A = A[~np.isnan(A)] but there is an error. I present the expected output.

import numpy as np
from numpy import nan
A=np.array([nan, '[1]', nan, '[2]', nan, '[3]', nan, '[4]', nan, '[5]'])
A = A[~np.isnan(A)]
print(A)

The error is

in <module>
    A = A[~np.isnan(A)]

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

The expected output is

array(['[1]', '[2]', '[3]', '[4]', '[5]'])
0 Answers
Related