Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.vstack

Stack arrays vertically by concatenating along axis 0. One-dimensional inputs are first treated as single rows, producing an output with at least two dimensions.

How it works

np.vstack combines arrays along the first axis. Before combining, each one-dimensional input is treated as a single row, so the result is at least two-dimensional; higher-dimensional inputs must agree in every non-first dimension.

Syntax

numpy.vstack(tup, *, dtype=None, casting='same_kind')

Parameters

NameTypeDescription
tupsequence of ndarraysSequence of ndarrays to stack; inputs must have compatible shapes.
dtypedtype, optionalOutput data type, if specified.
casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optionalCasting rule to apply when converting inputs to the requested output type.

Returns

stacked: ndarray — the array formed by stacking the inputs; it is at least two-dimensional.

Key points

  • It combines a sequence of arrays by placing them one after another as rows along the first axis.
  • Conceptually, it performs concatenation on axis 0 after adapting one-dimensional inputs.
  • Each one-dimensional input is treated as a one-row array before stacking.
  • The returned ndarray always has at least two dimensions.
  • The signature exposes optional dtype selection and a casting policy.

Common mistakes

  • One-dimensional inputs must contain the same number of elements.
  • For arrays with two or more dimensions, shapes may differ only along axis 0.

Example: Stack one-dimensional arrays as rows

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.vstack((a, b))
print(result)

This converts two one-dimensional arrays into rows and combines them into one two-dimensional array.

When to use it

Build a row-wise data matrix

Combine equal-length feature or measurement vectors into a matrix whose rows represent observations.

Join batches

Append compatible batches along their leading dimension while retaining the remaining dimensions.

Promote vectors to rows

Turn several one-dimensional sequences into a two-dimensional array for downstream ndarray operations.

Interview questions

QWhat does np.vstack do with one-dimensional inputs?

It stacks arrays along the first axis, treating each one-dimensional input as shape (1, N) first. Consequently, one-dimensional inputs become rows and the output has at least two dimensions.

Follow-up: How does it differ from np.stack? np.vstack concatenates along the first axis after normalizing one-dimensional inputs, whereas np.stack creates a new axis.

QWhat shape requirements must inputs satisfy?

The input is a sequence of ndarrays. Arrays must match in all dimensions except the first; one-dimensional arrays must have equal lengths.

Follow-up: What happens if the non-first dimensions do not match? The operation cannot form a valid concatenation and raises a shape-related error.

QWhat optional type controls does np.vstack provide?

The function accepts optional dtype and casting keyword arguments in the current stable signature. dtype requests the result type, while casting controls permitted conversions between input types.

Follow-up: When would casting='same_kind' reject an operation? It can reject conversions considered unsafe under the same-kind casting rule.

  • numpy.concatenate
  • numpy.stack
  • numpy.block

Source: numpy.vstack. Checked 2026-10-15.