haneulkim
haneulkim

Reputation: 4928

different element data types within numpy array?

Just like list in python where [1,"hello", {"python": 10}] it can have all different types within, can numpy array have this as well?

when numpyarray.dtype => dtype('float64') is it implying all elements are of type float?

Upvotes: 0

Views: 3987

Answers (2)

NoSplitSherlock
NoSplitSherlock

Reputation: 615

From the docs:

dtype : data-type, optional

The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. This argument can only be used to ‘upcast’ the array. For downcasting, use the .astype(t) method.

So if you set dtype as float64, everything needs to be a float. You can mix types, but then you can't set it as a mismatching type. It will use a type that will fit all data, like a string for example in the case of array(['1', 'Foo', '3.123']).

Upvotes: 1

fountainhead
fountainhead

Reputation: 3722

Yes, if you use numpy structured arrays, each element of the array would be a "structure", and the fields of the structure can have different datatypes.

The answer to your second question is yes. When the dtype attribute shows a value of float64, it means each element is a float64

Upvotes: 1

Related Questions