How do you save and load NumPy arrays with .npy and .npz files, and how do they compare with loadtxt/genfromtxt?

Quick Overview

Asks how to save and load NumPy arrays with the binary .npy and .npz formats and how they compare with text files read by loadtxt or genfromtxt. It tests what each format preserves, how to handle large arrays and missing values, and when to choose each one.

How do you save and load NumPy arrays with .npy and .npz files, and how do they compare with loadtxt/genfromtxt?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How do you save and load NumPy arrays to disk using `.npy` and `.npz` files, and how does that differ from text formats read with `loadtxt` or `genfromtxt`? ```hint Try a round trip Picture saving a 3D `int32` array each way and loading it back. List what you get back in each case and what you had to tell NumPy again. ``` ### What a Strong Answer Covers - The calls for saving and loading a single array and a set of named arrays - What the binary formats preserve, and how loading a `.npz` file behaves - What text formats lose or make you restate, and when `genfromtxt` is needed instead of `loadtxt` - File size, speed and portability trade-offs, with a rule for choosing ### Follow-up Questions - How would you work with a 20 GB array on disk that does not fit in memory? - Why does `np.load` refuse object arrays by default, and when is it safe to change that? - When would you choose Parquet, HDF5 or Zarr over `.npy` files? - Does `savetxt` with its default format lose float64 precision?

Overview: Asks how to save and load NumPy arrays with the binary .npy and .npz formats and how they compare with text files read by loadtxt or genfromtxt. It tests what each format preserves, how to handle large arrays and missing values, and when to choose each one.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

How do you save and load NumPy arrays to disk using .npy and .npz files, and how does that differ from text formats read with loadtxt or genfromtxt?

What a Strong Answer Covers Guidance

  • The calls for saving and loading a single array and a set of named arrays
  • What the binary formats preserve, and how loading a .npz file behaves
  • What text formats lose or make you restate, and when genfromtxt is needed instead of loadtxt
  • File size, speed and portability trade-offs, with a rule for choosing

Follow-up Questions Guidance

  • How would you work with a 20 GB array on disk that does not fit in memory?
  • Why does np.load refuse object arrays by default, and when is it safe to change that?
  • When would you choose Parquet, HDF5 or Zarr over .npy files?
  • Does savetxt with its default format lose float64 precision?