Reputation: 7408
I have few numpy arrays, which can be formatted as
[1.525, 2.565, 6.367, ...] # elements are float numbers
or
['', '', '', ...] # elements are empty strings
I'd like to find out if all the elements in an array are of the same data type.
For now, I am using:
if isinstance(np.any(time_serie),float):
return sum(time_serie)
But this one doesn't work. I got following error:
TypeError: cannot perform reduce with flexible type
So, may I know how to work around this? Thanks.
Upvotes: 1
Views: 2632
Reputation: 17933
If you're looking for a particular data-type as provided in your example, e.g. all items are floats, then a map and reduce will do the trick:
>>> x = [1.525, 2.565, 6.367]
>>> all(map(lambda i: isinstance(i, float), x))
True
>>> x = [1.525, 2.565, '6.367']
>>> all(map(lambda i: isinstance(i, float), x))
False
Upvotes: 2
Reputation: 35741
You might want to use a list comprehension or map()
for creating a sequence of data types, then make a set
from this sequence and see if the length of the set is 1.
Upvotes: 0