I think you might want to do the following, depending on what's inside the object type and if there's no padding to worry about:

bane.view(np.complex64) or
bane.view(np.complex128)

However if that does not work, which it didn't for some small tuple example I tried, the following worked:

bane.astype(np.float).view(np.complex64)

Consider using numpy structures rather than objects for the base dtype, you may have an easier time over all.

Answer from Jason Newton on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.4 Manual
Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128. The other data-types do not have Python equivalents.
🌐
NumPy
numpy.org › doc › 1.25 › user › basics.types.html
Data types — NumPy v1.25 Manual
Once you have imported NumPy using >>> import numpy as np the dtypes are available as np.bool_, np.float32, etc. Advanced types, not listed above, are explored in section Structured arrays. There are 5 basic numerical types representing booleans (bool), integers (int), unsigned integers (uint) floating point (float) and complex.
🌐
NumPy
numpy.org › doc › stable › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.4 Manual
The first argument is any object that can be converted into a fixed-size data-type object. The second argument is the desired shape of this type. If the shape parameter is 1, then the data-type object used to be equivalent to fixed dtype. This behaviour is deprecated since NumPy 1.17 and will ...
🌐
Medium
medium.com › @whyamit404 › working-with-complex-numbers-in-numpy-c3eae8876a88
Working with Complex Numbers in NumPy | by whyamit404 | Medium
February 8, 2025 - It’s easy to create a complex number array in NumPy. But if you need to make sure that your array is specifically of a complex type (instead of the default type), you can do so by setting the dtype to complex.
🌐
W3Schools
w3schools.com › python › numpy › numpy_data_types.asp
NumPy Data Types
c - complex float · m - timedelta · M - datetime · O - object · S - string · U - unicode string · V - fixed chunk of memory for other type ( void ) The NumPy array object has a property called dtype that returns the data type of the array: Get ...
🌐
GitHub
github.com › numpy › numpy › issues › 12184
Need extra dtypes: complex integer with different bit rate (8, 10 and 12 bits) · Issue #12184 · numpy/numpy
October 16, 2018 - 01 - Enhancementcomponent: numpy.dtype · Felix-neko · opened · on Oct 16, 2018 · Issue body actions · Hi all! I'm modeling fixed-point calculations in my radar signal processing unit and need to use complex integer values with small bitrate (8, 10 and 12 bits).
Author   Felix-neko
🌐
Ufkapano
ufkapano.github.io › scicomppy › week08 › np_dtype.html
NumPy - data type objects
np.sqrt(np.array([-1, 0, 1])) # default dtype=float # RuntimeWarning: invalid value encountered in sqrt # array([nan, 0., 1.]) np.sqrt(np.array([-1, 0, 1], dtype=complex)) # array([0.+1.j, 0.+0.j, 1.+0.j]) a = np.array([1, 2, 3], dtype=complex) a.real # or np.real(a) # array([1., 2., 3.]) a.imag # or np.imag(a) # array([0., 0., 0.])
🌐
Stack Overflow
stackoverflow.com › questions › 46344549 › how-to-specify-dtype-correctly-in-python-when-dealing-with-complex-numbers-and-n
How to specify dtype correctly in python when dealing with complex numbers and numpy? - Stack Overflow
And the second argument for array (and matrix) is interpreted by NumPy as the dtype: np.array([[complex(1/math.sqrt(2)), cmath.exp(1j) ], [-cmath.exp(-1j).conjugate(), complex(1/math.sqrt(2))]], dtype=complex) # array([[ 0.70710678+0.j , 0.54030231+0.84147098j], # [-0.54030231-0.84147098j, 0.70710678+0.j ]]) Share ·
Find elsewhere
Top answer
1 of 3
19

I also deal with lots of complex integer data, generally basebanded data.

I use

dtype = np.dtype([('re', np.int16), ('im', np.int16)])

It's not perfect, but it adequately describes the data. I use it for loading into memory without doubling the size of the data. It also has the advantage of being able to load and store transparently with HDF5.

DATATYPE  H5T_COMPOUND {
    H5T_STD_I16LE "re";
    H5T_STD_I16LE "im";
}

Using it is straightforward and is just different.

x = np.zeros((3,3),dtype)
x[0,0]['re'] = 1
x[0,0]['im'] = 2
x
>> array([[(1, 2), (0, 0), (0, 0)],
>>        [(0, 0), (0, 0), (0, 0)],
>>        [(0, 0), (0, 0), (0, 0)]],
>>  dtype=[('re', '<i2'), ('im', '<i2')])

To do math with it, I convert to a native complex float type. The obvious approach doesn't work, but it's also not that hard.

y = x.astype(np.complex64) # doesn't work, only gets the real part
y = x['re'] + 1.j*x['im']  # works, but slow and big
y = x.view(np.int16).astype(np.float32).view(np.complex64)
y
>> array([[ 1.+2.j,  0.+0.j,  0.+0.j],
>>        [ 0.+0.j,  0.+0.j,  0.+0.j],
>>        [ 0.+0.j,  0.+0.j,  0.+0.j]], dtype=complex64)

This last conversion approach was inspired by an answer to What's the fastest way to convert an interleaved NumPy integer array to complex64?

2 of 3
2

Consider using matrices of the form [[a,-b],[b,a]] as a stand-in for the complex numbers.

Ordinary multiplication and addition of matrices corresponds to addition an multiplication of complex numbers (this subring of the collection of 2x2 matrices is isomorphic to the complex numbers).

I think Python can handle integer matrix algebra.

🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-data-types
Numpy data Types - GeeksforGeeks
July 23, 2025 - import numpy as np arr1 = np.array([1, 2, 3, 4], dtype=np.float64) # Creating a 3x3 int32 array of zeros arr2 = np.zeros((3, 3), dtype=np.int32) # Creating a 2x2 complex128 array of ones arr3 = np.ones((2, 2), dtype=np.complex128) # Creating a 1D bool array arr4 = np.empty((4,), dtype=np.bool_) # Print the arrays and their data types print(arr1.dtype) print(arr2.dtype) print(arr3.dtype) print(arr4.dtype) Output ·
🌐
GitHub
github.com › numpy › numpy › issues › 11993
Python 3.7 can cast object to complex type · Issue #11993 · numpy/numpy
September 19, 2018 - Reproducing code example: import numpy as np print(np.__version__) arr = np.array(['AAAAA', 18465886.0, 18465886.0], dtype=object) print(arr.astype(np.complex64)) print(arr.astype(np.complex64)) Error message: Python3.6 works as I would ...
Author   bear24rw
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: astype() to change dtype of an array | note.nkmk.me
February 4, 2024 - NumPy arrays (ndarray) cannot be specified. You need to specify either the data type of the array or provide a specific value instead. a = np.array([1, 2, 3], dtype=np.int8) print(type(a)) # <class 'numpy.ndarray'> # print(np.iinfo(a)) # ValueError: Invalid integer data type 'O'. print(np.iinfo(a.dtype)) # Machine parameters for int8 # --------------------------------------------------------------- # min = -128 # max = 127 # --------------------------------------------------------------- # print(np.iinfo(a[0])) # Machine parameters for int8 # --------------------------------------------------------------- # min = -128 # max = 127 # --------------------------------------------------------------- #
🌐
NumPy
numpy.org › doc › stable › reference › c-api › dtype.html
Data type API — NumPy v2.4 Manual
On the Python side, we still support np.dtype('p') to fetch a dtype compatible with storing pointers, while n is the correct character for the ssize_t. ... The C size_t/Py_size_t. ... There are also typedefs for signed integers, unsigned integers, floating point, and complex floating point ...
🌐
GitHub
github.com › numpy › numpy › issues › 17325
ENH: add a canonical way to determine if dtype is integer, floating point or complex · Issue #17325 · numpy/numpy
September 16, 2020 - There is currently no good way (AFAIK) to figure out if the dtype of an array is integer, floating point or complex. Right now one of these is the most common probably: x.dtype.kind in np.typecodes["AllFloat"] np.issubdtype(x.dtype, np.f...
Author   rgommers
🌐
GitHub
github.com › numba › numba › issues › 1563
Support dtype=np.complex in np.zeros · Issue #1563 · numba/numba
December 2, 2015 - The following code crashes : import numpy as np from numba import njit @njit def build_array(N): return np.zeros((2*N, 2*N), dtype=np.complex) with the following error : Failed at nopython (nopytho...
Author   MatthieuDartiailh
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_data_types.htm
NumPy - Data Types
In this case, NumPy raises a ComplexWarning and discards the imaginary part during conversion − · Original array: [1.+2.j 3.+4.j 5.+6.j] Original dtype: complex128 ComplexWarning: Casting complex values to real discards the imaginary partc_float = c.astype(np.float32) Converted array: [1.