NumPy Structured Arrays
Last Updated: 09 Nov 2025
Structured arrays let you store heterogeneous data (like CSV, sensor logs, database rows) in a single NumPy array — fast, memory-efficient, vectorized.
Hinglish Tip: “Structured array = ek hi array me naam, umar, salary — jaise Excel sheet!”
1. Define dtype with Named Fields
import numpy as np
# Define schema: name (string), age (int), salary (float)
dt = np.dtype([
('name', 'U10'), # Unicode string, max 10 chars
('age', 'i4'), # 32-bit integer
('salary', 'f8') # 64-bit float
])
# Create array
employees = np.array([
('Amit', 25, 50000.0),
('Suraj', 30, 75000.0),
('Raj', 28, 65000.0)
], dtype=dt)
print(employees)
Output:
[('Amit', 25, 50000.) ('Suraj', 30, 75000.) ('Raj', 28, 65000.)]
2. Access by Field Name
print("Names:", employees['name'])
print("Ages:", employees['age'])
print("Salaries:", employees['salary'])
3. Vectorized Operations
# 10% bonus for all
employees['salary'] *= 1.10
print(employees['salary'])
# Filter: age > 28
print(employees[employees['age'] > 28])
4. Nested & Complex dtype
complex_dt = np.dtype([
('id', 'i4'),
('position', 'f4', (3,)), # 3D point
('active', '?')
])
point = np.array([(1, [10.0, 20.0, 30.0], True)], dtype=complex_dt)
print(point['position'])
5. clip
- Cuts values that are too small or too big.
- arr = np.clip(arr, min_value, max_value)
import numpy as np
x = np.array([-10, -3, 0, 2, 7, 15, 100])
# 1. between 0 and 10
print(np.clip(x, 0, 10))
# → [ 0 0 0 2 7 10 10]
# 2. Only lower limit (everything below -2 becomes -2)
print(np.clip(x, -2, None))
# → [ -2 -2 -2 -2 2 7 100]
# 3. Only upper limit
print(np.clip(x, None, 8))
# → [-10 -3 0 2 7 8 8]
# 4. Clip in-place (faster, no copy)
x.clip(0, 10, out=x) # x is now changed
print(x)
# → [ 0 0 0 2 7 10 10]
6. pad
- Pads an array with a constant value.
- arr = np.pad(arr, pad_width, mode='constant', constant_values=0)
- mode can be 'constant', 'edge', 'reflect', 'symmetric'.
import numpy as np
small = np.array([[1, 2],
[3, 4]])
# Example 1 same padding on each side
padded = np.pad(small, pad_width=2, mode='constant', constant_values=0)
print(padded
# Example 2 – Different padding on each side
# (top, bottom, left, right)
padded = np.pad(small, pad_width=(3, 1, 2, 4), mode='constant', constant_values=9)
print(padded))
7. tile
- use to repeat an array.
- np.tile(your_array, (rows, cols))
import numpy as np
# 1 D
small = np.array([10, 20])
print(np.tile(small, 4))
# 2 D
block = np.array([[1, 2],
[3, 4]])
# Repeat: 3 times down, 2 times right
big = np.tile(block, (3, 2))
print(big)