Types and memory
Last updated
Every array has one data type, its dtype, and the dtype decides what numbers fit and how much memory they take. In this lesson you'll read and change dtypes, work out what an array costs, meet nan, and see what happens when a number is too big for its type.
By the end of this lesson you can
- Read an array's
dtypeand change it withastype - Work out an array's size in memory from its dtype and length
- Explain why
nanneeds a float array - Spot overflow: a number too big for its type wrapping round with no error
The common types
Run the cells: NumPy picks a dtype from what you give it.
The four you'll meet most:
| dtype | holds | bytes each |
|---|---|---|
int64 | whole numbers, up to about 9 quintillion | 8 |
float64 | decimals, about 15 significant digits | 8 |
bool | True or False | 1 |
int8 | whole numbers from −128 to 127 | 1 |
(Again, this page shows int32 where your computer shows int64, so the first array here is 4 bytes a number, 12 in all. Lesson 2.1 says why.)
astype makes a copy with a new type. It doesn't change the original. Sort these columns by the type you'd give them:
0 of 5 placed right
Kilos of rice sold, always whole
Price per kilo, ₱45.50
Whether the store was open that day
Kilos sold, with some days not reported
Temperature at noon, 31.4 °C
What an array costs
An array's memory is easy to work out: the number of elements times the bytes each one takes. nbytes tells you. The last cell of the notebook makes a million zeros:
As float32, half that. For the data in this course, 8 MB is nothing. But a table of 50 million rows and 20 columns of float64 is 8 GB, more than many laptops have. Choosing smaller types is one of the ways to fit big data in memory, and you'll use it in Stage 4.
Here's how a number is stored inside those bytes, using the smallest type, int8:
int8 is one byte: 8 bits. Each bit is worth a power of 2, and in a signed type the first bit is worth −128. 100 is 64 + 32 + 4.nan is a float
Real data has gaps. NumPy marks a missing number with np.nan, "not a number". Run the cells.
Three things to remember:
nanis a float. The array[45, nan, 44]becamefloat64, even though 45 and 44 are whole numbers. An integer type has no way to say "missing", so the last cell fails: you can't putnaninto an integer array.nannever equals anything, not even itself.np.nan == np.nanisFalse, so you can't find gaps with==.- Use
np.isnaninstead. It givesTruewhere a value is missing.
pandas has better ways to mark missing values, which you'll meet in lesson 3.8. They're built on this.
Wrong way: prices in int8
You want to save memory, so you store kilos as int8, 1 byte each. Run it.
200 became −56 and 150 became −106, with no error and no warning. int8 stops at 127, as np.iinfo shows. Step back to the bit boxes in step 2 to see how 200's bits read as −56: the number goes past the top and wraps round to the bottom. This is called overflow.
Only 90 survived, because it fits. A total worked out from this array would be wrong, and nothing would tell you. Check the range with np.iinfo before you choose a small type; int16 (up to 32,767) is fine here.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- NumPy: Data types: every dtype, its size, and overflow
- NumPy: numpy.ndarray.astype: converting an array to another type
- NumPy: numpy.isnan: finding nan, which never equals itself
- The Python Tutorial: Floating-Point Arithmetic: Issues and Limitations: how floats are stored