Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Pipe

ndarray.imag

ndarray.imag gives the imaginary component of an array. It returns a view for complex arrays, zeros with the same dtype for non-complex arrays, and elementwise values for object arrays.

How it works

Accessing an ndarray’s imag attribute produces the imaginary component element by element. Complex arrays expose this as a view into the source array, while non-complex arrays produce zeros with the same dtype; object arrays use each element’s imag attribute when available.

Syntax

ndarray.imag

Returns

An ndarray containing the imaginary part; for complex arrays it is a view, for non-complex arrays it is zeros of the same dtype, and for object arrays it is elementwise .imag or 0 when unavailable.

Key points

  • The attribute represents the imaginary component of an ndarray.
  • Complex inputs provide a view, enabling access to imaginary components without making a separate array copy.
  • Non-complex inputs produce an all-zero result that preserves the source dtype.
  • Object-dtype inputs process each stored object and fall back to zero when its imag attribute is missing.
  • The documented example demonstrates inspecting the dtype of the extracted imaginary-part array.

Common mistakes

  • For complex arrays, imag is a view rather than an independent copy, so changes through it can affect the original array.
  • Real or otherwise non-complex arrays yield zeros, not a floating-point conversion of the original values.
  • Object arrays inspect each element individually and use zero when an element has no imag attribute.

Example: Extract imaginary values

import numpy as np

x = np.sqrt(np.array([1 + 0j, 0 + 1j]))
print(x.imag)       # [0.         0.70710678]
print(x.imag.dtype) # float64

This extracts the imaginary values from a complex array and displays the resulting values and dtype.

When to use it

Process complex signals

Separate phase-related or quadrature data from complex-valued numerical arrays.

Validate complex results

Inspect whether complex computations produced expected imaginary components.

Handle object arrays

Read per-object imaginary values when arrays use object dtype.

Interview questions

QWhat does ndarray.imag provide?

ndarray.imag is an attribute that extracts an array’s imaginary part. For complex arrays, the result is a view of the original array.

Follow-up: What happens for a real-valued array? It returns a zero array with the same dtype rather than a complex result.

QIs ndarray.imag always a copy?

A complex array’s imag result shares storage with the source array. Therefore, modifying that result may modify the corresponding imaginary components of the source.

Follow-up: How does this differ from object arrays? Object arrays evaluate each element’s imag attribute independently, falling back to zero when absent.

QHow does behavior vary by array dtype?

For non-complex arrays, imag returns zeros with the original array’s dtype. For object arrays, it instead obtains each element’s imag value or uses zero if unavailable.

Follow-up: What dtype should be expected for a complex input? It is determined by the complex array’s component representation, as illustrated by the documented float64 example.

  • numpy.ndarray.real
  • numpy.imag
  • numpy.ndarray

Source: numpy.ndarray.imag — NumPy v2.4 Manual. Checked 2026-10-15.