Where can I find a full description of the "format" field for NumPy objects that includes 'Z' for "complex"?

Viewed 106

When I look at the "format" field for a complex double NumPy object, I see "Zd":

>>> import numpy as np
>>> ac = np.array([[1,2]], dtype=complex)
>>> memoryview(ac).format
'Zd'

However, I don't see "Z" mentioned in the official Python documentation for "struct", and I have also been unable to find it in the NumPy documentation, though it's possibly I simply missed it because I couldn't think of anything more likely to find relevant hits than the terms "complex", "Z", and "format", all of which bring up a lot of irrelevant information. Could anyone point me to relevant documentation and/or give me their own description?

2 Answers

The given output represents the format specifiers returned from given Python's memoryview object

Zd is apparently the format string for NumPy's (scalar) data type "complex double" np.cdouble.

Related Documentation and Insights

Each of NumPy's data-type defines similar literals as Character code, see documentation of dtype

A unique character code for each of the 21 different built-in types.

Among the scalar data-types:

  • a single lower-case character is used for unsigned integers and floating-point primitives.

  • a single upper-case character is used for signed integers and complex floating-point primitives.

For example:

The key documentation is in PEP 3118, section "Additions to the struct string-syntax", specifically this line:

'Z' complex (whatever the next specifier is)

Thanks to Zev for helping me find the way to that page.

As far as I can tell, the NumPy documentation does mention the unique character codes that specify the real data types (as hc_dev mentioned), but not the fact that in general, the derived complex data types are prefaced by "Z".

Related