Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

TypeError: Cannot convert numpy.ndarray to numpy.ndarray

Verified FixPython 3.10+NumPy 1.24+Silo: numpy

Quick Fix / Solution Rapide

Pad nested sub-arrays to uniform dimensions or specify dtype=object when creating inhomogeneous multidimensional arrays.

Root Cause Analysis

This error occurs when Python tries to coerce or convert an existing numpy.ndarray (or an object array containing sub-arrays) into a new numeric ndarray when the constituent elements have incompatible shapes or conflicting internal dtypes.

1. Ragged and Inhomogeneous Nested Arrays

NumPy arrays require a homogeneous, contiguous memory block where every row has an identical number of columns. When developers create a list of arrays with varying lengths (e.g., [np.array([1, 2]), np.array([3, 4, 5])]) and attempt to force conversion into a 2D numeric array, NumPy cannot establish a uniform shape and raises a TypeError or ValueError.

2. Object Array Casting Confusion

When an array is created with dtype=object holding nested ndarray objects, calling .astype(np.float64) fails because NumPy attempts to cast each individual ndarray object as if it were a single scalar float.

3. Subclass Incompatibilities and Buffer Exports

Custom subclasses of np.ndarray that do not properly implement the array interface or memory buffer protocol can trigger conversion errors when passed into functions expecting standard contiguous C-arrays.

4. Modern NumPy Strict Ragged Array Protections

Starting in NumPy 1.24+, creating ragged arrays without explicitly declaring dtype=object is strictly forbidden, preventing accidental creation of broken multi-dimensional data structures.

Reproduction Code (MCVE)

Example: Bug Reproduction
import numpy as np

nested_ragged = np.array([np.array([1, 2]), np.array([3, 4, 5])], dtype=object)

def strict_matrix_conversion(arr):
    if any(len(row) != len(arr[0]) for row in arr):
        raise TypeError('Cannot convert numpy.ndarray to numpy.ndarray: inhomogeneous sub-array dimensions')
    return np.array(arr.tolist(), dtype=np.int64)

strict_matrix_conversion(nested_ragged)

Solution 1: Pad Inhomogeneous Sub-Arrays to Uniform Length

Pad shorter sub-arrays with zeros or sentinel values using np.pad() to create a uniform 2D matrix.

Example: Recommended Solution
import numpy as np

sub_arrays = [np.array([1, 2]), np.array([3, 4, 5])]
max_len = max(len(row) for row in sub_arrays)

padded_matrix = np.array([
    np.pad(row, (0, max_len - len(row)), constant_values=0)
    for row in sub_arrays
], dtype=np.int64)

print(f'Padded 2D Matrix:\n{padded_matrix}')
print(f'Shape: {padded_matrix.shape}, Dtype: {padded_matrix.dtype}')

Solution 2: Concatenate or Flatten into a 1D Array

If rectangular dimensions are not mandatory, concatenate all nested sub-arrays into a single contiguous 1D array.

Example: Alternative Solution
import numpy as np

sub_arrays = [np.array([1, 2]), np.array([3, 4, 5])]
flat_array = np.concatenate(sub_arrays)
print(f'Concatenated 1D Array: {flat_array}')
print(f'Total elements: {len(flat_array)}')

A common trap is attempting to convert a 2D object array containing arrays by running .astype(float). This fails because .astype() expects scalar elements, not nested container objects. Always convert nested containers using list comprehensions or np.vstack() / np.pad() before casting. Another edge case involves handling missing data: if some rows contain None instead of arrays, np.concatenate will fail with a TypeError. Filter out or replace None with empty arrays np.array([]) prior to concatenation. Contrast this error with ValueError: setting an array element with a sequence, which occurs when assigning a sub-sequence into a scalar slice of a matrix.