---
title: "Explain the NumPy Data Types."  
description: "Explain the NumPy Data Types."  
author: "Ravi Vishwakarma"  
published: 2025-11-09  
updated: 2025-11-09  
canonical: https://www.mindstick.com/interview/34409/explain-the-numpy-data-types  
category: "python"  
tags: ["python-3.4", "numpy"]  
reading_time: 5 minutes  

---

# Explain the NumPy Data Types.

> In **NumPy**, data types (called `dtypes`) define the kind of elements stored in an array — such as integers, floats, booleans, or custom data. NumPy provides **finer control** over data types than standard Python because it’s designed for **performance and memory efficiency** in numerical computing.

Let’s go over it clearly:

## 1. What Are NumPy Data Types?

A **NumPy data type (**`dtype`**)** describes the **type of value** and **memory size** each element in an array uses.

Example:

```python
import numpy as np

arr = np.array([1, 2, 3], dtype=np.int32)
print(arr.dtype)
```

## Output:

```plaintext
int32
```

This means each integer takes 4 bytes (32 bits).

## 2. Categories of NumPy Data Types

| **Category** | **Description** | **Examples of dtype** |
| --- | --- | --- |
| **Integer** | Whole numbers (positive/negative) | `int8`, `int16`, `int32`, `int64` |
| **Unsigned Integer** | Whole numbers (only positive) | `uint8`, `uint16`, `uint32`, `uint64` |
| **Float** | Decimal numbers | `float16`, `float32`, `float64` |
| **Complex** | Complex numbers (a + bj) | `complex64`, `complex128` |
| **Boolean** | True/False values | `bool_` |
| **String** | Fixed-size byte or Unicode strings | `string_`, `unicode_` |
| **Object** | Python objects (mixed types) | `object_` |
| **Datetime** | Date and time values | `datetime64` |
| **Timedelta** | Difference between two datetime values | `timedelta64` |

## 3. Examples

### Integers

```python
np.array([10, 20, 30], dtype=np.int8)   # 1 byte per element
np.array([10, 20, 30], dtype=np.int64)  # 8 bytes per element
```

### Unsigned Integers

```python
np.array([0, 255], dtype=np.uint8)  # 0–255 range
```

### Floats

```python
np.array([3.14, 2.71], dtype=np.float32)
```

### Complex Numbers

```python
np.array([1+2j, 3+4j], dtype=np.complex128)
```

### Boolean

```python
np.array([True, False, True], dtype=np.bool_)
```

### String

```python
np.array(['Hello', 'World'], dtype=np.string_)   # ASCII
np.array(['Hello', 'World'], dtype=np.unicode_)  # Unicode
```

### Datetime

```python
np.array(['2024-01-01', '2024-02-01'], dtype='datetime64')
```

### Object

```python
np.array([1, 'two', 3.0], dtype=object)
```

## 4. Checking and Converting Data Types

### Check the dtype

```python
arr = np.array([1.2, 3.4, 5.6])
print(arr.dtype)
```

### Convert dtype

```python
arr_int = arr.astype(np.int32)
print(arr_int, arr_int.dtype)
```

## Output:

```plaintext
[1 3 5] int32
```

## 5. Why NumPy Has Its Own Data Types

Python’s native types (`int`, `float`, etc.) are **object-based**, meaning they take more memory and are slower for numerical operations.\
NumPy’s types are **fixed-size, C-like types** designed for **fast computation and minimal memory usage**.

For example:

| Type | Bytes | Range |
| --- | --- | --- |
| `int8` | 1 | -128 to 127 |
| `int16` | 2 | -32,768 to 32,767 |
| `int32` | 4 | -2,147,483,648 to 2,147,483,647 |
| `float64` | 8 | ~15–16 decimal digits precision |

## 6. Custom Structured Data Types

You can define **your own compound data type** (like a record or struct):

```python
person_dtype = np.dtype([
    ('name', 'U10'),
    ('age', 'i4'),
    ('height', 'f4')
])

people = np.array([
    ('Alice', 25, 5.5),
    ('Bob', 30, 6.0)
], dtype=person_dtype)

print(people['name'])
print(people['age'])
```

## Answers

### Answer by Ravi Vishwakarma

> In **NumPy**, data types (called `dtypes`) define the kind of elements stored in an array — such as integers, floats, booleans, or custom data. NumPy provides **finer control** over data types than standard Python because it’s designed for **performance and memory efficiency** in numerical computing.

Let’s go over it clearly:

## 1. What Are NumPy Data Types?

A **NumPy data type (**`dtype`**)** describes the **type of value** and **memory size** each element in an array uses.

Example:

```python
import numpy as np

arr = np.array([1, 2, 3], dtype=np.int32)
print(arr.dtype)
```

## Output:

```plaintext
int32
```

This means each integer takes 4 bytes (32 bits).

## 2. Categories of NumPy Data Types

| **Category** | **Description** | **Examples of dtype** |
| --- | --- | --- |
| **Integer** | Whole numbers (positive/negative) | `int8`, `int16`, `int32`, `int64` |
| **Unsigned Integer** | Whole numbers (only positive) | `uint8`, `uint16`, `uint32`, `uint64` |
| **Float** | Decimal numbers | `float16`, `float32`, `float64` |
| **Complex** | Complex numbers (a + bj) | `complex64`, `complex128` |
| **Boolean** | True/False values | `bool_` |
| **String** | Fixed-size byte or Unicode strings | `string_`, `unicode_` |
| **Object** | Python objects (mixed types) | `object_` |
| **Datetime** | Date and time values | `datetime64` |
| **Timedelta** | Difference between two datetime values | `timedelta64` |

## 3. Examples

### Integers

```python
np.array([10, 20, 30], dtype=np.int8)   # 1 byte per element
np.array([10, 20, 30], dtype=np.int64)  # 8 bytes per element
```

### Unsigned Integers

```python
np.array([0, 255], dtype=np.uint8)  # 0–255 range
```

### Floats

```python
np.array([3.14, 2.71], dtype=np.float32)
```

### Complex Numbers

```python
np.array([1+2j, 3+4j], dtype=np.complex128)
```

### Boolean

```python
np.array([True, False, True], dtype=np.bool_)
```

### String

```python
np.array(['Hello', 'World'], dtype=np.string_)   # ASCII
np.array(['Hello', 'World'], dtype=np.unicode_)  # Unicode
```

### Datetime

```python
np.array(['2024-01-01', '2024-02-01'], dtype='datetime64')
```

### Object

```python
np.array([1, 'two', 3.0], dtype=object)
```

## 4. Checking and Converting Data Types

### Check the dtype

```python
arr = np.array([1.2, 3.4, 5.6])
print(arr.dtype)
```

### Convert dtype

```python
arr_int = arr.astype(np.int32)
print(arr_int, arr_int.dtype)
```

## Output:

```plaintext
[1 3 5] int32
```

## 5. Why NumPy Has Its Own Data Types

Python’s native types (`int`, `float`, etc.) are **object-based**, meaning they take more memory and are slower for numerical operations.\
NumPy’s types are **fixed-size, C-like types** designed for **fast computation and minimal memory usage**.

For example:

| Type | Bytes | Range |
| --- | --- | --- |
| `int8` | 1 | -128 to 127 |
| `int16` | 2 | -32,768 to 32,767 |
| `int32` | 4 | -2,147,483,648 to 2,147,483,647 |
| `float64` | 8 | ~15–16 decimal digits precision |

## 6. Custom Structured Data Types

You can define **your own compound data type** (like a record or struct):

```python
person_dtype = np.dtype([
    ('name', 'U10'),
    ('age', 'i4'),
    ('height', 'f4')
])

people = np.array([
    ('Alice', 25, 5.5),
    ('Bob', 30, 6.0)
], dtype=person_dtype)

print(people['name'])
print(people['age'])
```


---

Original Source: https://www.mindstick.com/interview/34409/explain-the-numpy-data-types

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
