How to mix strings and floats in a NumPy array?
I'd consider a pandas DataFrame instead - the actual data is stored as a numpy array and so supports everything it needs to, and the headers are separate so you don't need to do awkward slicing when working on the whole data set. You can also then index rows and columns by their index. And it supports going to and from CSVs easily.
As an aside, the way to get arrays to accept mixed types is
np.array([1, 'a'], dtype=object)
but obviously you lose practically all of the benefits of using numpy in the first place.
More on reddit.comWhat’s the deal with arrays in Python?
How do I create an array in Python?
How to declare an array in Python?
What is an array in Python, and how do I use it?
Videos
As Carcigenicate pointed out, you can use array.array directly as a type annotation. However, you can't use array.array[int] or array.array[float] to specify the type of the elements.
If you need to do this, my suggestion is to use MutableSequence from collections.abc, since arrays implement all of the necessary operations: __len__, __getitem__, __setitem__, __contains__, __iter__, etc.
from collections.abc import MutableSequence
arr: MutableSequence[float] = array.array('f')
Starting from a sufficiently new Python version, Python and type checkers should accept array.array[int] and array.array[float]. See https://github.com/python/cpython/pull/98661 (runtime behavior) and https://github.com/python/typeshed/pull/1649 (type stub)
Taking the example from https://github.com/python/mypy/issues/13942, the following works now:
from array import array
x: array = array("L")
y: array[int] = array("L")