0

I have a bunch of numpy arrays, and i'm trying to check the type of the values within each array as some of them contain stars which cause the entire array to become of type string and others are floats. I want to be able to check an array and see if it contains floats or strings. Code below:

nums = np.array([123, 54, 645, 89, 465, 98])
nums2 = np.array(['4', '987', '*65*', '89'])

Is there a way to write some kind of loop to check this? My ideal output would be along the lines of:

nums = float
nums2 = string
KGreen
  • 123
  • 5
  • 9
  • 1
    In your example `nums` and `nums2` aren't numpy arrays but plain python lists. Did you mean `nums = np.array([123, 54, 645, 89, 465, 98])`? – Pavel Mar 21 '20 at 21:12
  • I do sorry let me edit that – KGreen Mar 21 '20 at 21:14
  • 1
    Does this answer your question? [check type within numpy array](https://stackoverflow.com/questions/40312013/check-type-within-numpy-array) – Tomerikoo Mar 21 '20 at 21:15
  • 1
    `nums2` isn't valid python. Are you missing some quotes? – hpaulj Mar 21 '20 at 21:29
  • Does the source list contain numbers or strings? The starred element has to be a string, but what of the others? What's the dtype of the arrays? – hpaulj Mar 22 '20 at 01:10

1 Answers1

2

What you have there are lists. To make a NumPy array, you need to use its constructor as:

import numpy as np
nums = np.array([123, 54, 645, 89, 465, 98])
nums2 = np.array(['45', '987', '65', '89'])

NumPy arrays have an attribute called dtype. You can use that to see what type of data you have in your arrays.

>>> nums.dtype
dtype('int64')
>>> nums2.dtype
dtype('<U3')

For more info on NumPy data types: https://docs.scipy.org/doc/numpy/user/basics.types.html