NumPy · lesson 3 of 13
Parts in this lesson
Aarr.dtype2.1: the type of every element
Barr.astype(np.float64)a copy with another type
Carr.nbyteshow many bytes it takes
Dnp.nannot a number: a missing value

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 dtype and change it with astype
  • Work out an array's size in memory from its dtype and length
  • Explain why nan needs 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.

dtypes.ipynb

The four you'll meet most:

dtypeholdsbytes each
int64whole numbers, up to about 9 quintillion8
float64decimals, about 15 significant digits8
boolTrue or False1
int8whole numbers from −128 to 1271

(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

  1. Kilos of rice sold, always whole

  2. Price per kilo, ₱45.50

  3. Whether the store was open that day

  4. Kilos sold, with some days not reported

  5. 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:

1000000×8 bytes=8000000 bytes=8 MB

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:

One number in 8 bits. An 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.01100100np.int8(100)−1286432168421
1 of 4 An 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.

missing.ipynb

Three things to remember:

  • nan is a float. The array [45, nan, 44] became float64, 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 put nan into an integer array.
  • nan never equals anything, not even itself. np.nan == np.nan is False, so you can't find gaps with ==.
  • Use np.isnan instead. It gives True where 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.

small.ipynb

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

How many bytes does np.zeros(500) take? It’s float64.
week = np.array([45, 44, np.nan]). How do you find which day is missing?
np.array([100, 130]).astype(np.int8). What happens to 130?

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.

All sources for Simple Data Analysis