0

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?

2
  • Why do you want an array with mixed data types? Why not stick with a list? Commented Mar 3, 2019 at 17:18
  • I just wanted to know. Since incoming data can be of many different types I wanted to know what happens when I convert it to numpy array Commented Mar 3, 2019 at 17:21

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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']).

3 Comments

dtype for array([1, 1.2, "foo"]) give me "<U32". this implies all elements fall in dtype <U32? and what is "<U32" data type?
Thank you for your help!
@HaneulKim You're right, I forgot to actually write them down as strings. Edited the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.