Quick answer: This NumPy error usually means a scalar array slot received a sequence or nested rows have incompatible lengths. Inspect the target shape, input nesting, dtype, and failing index, then normalize the data or make an intentional object-array decision.

ValueError: setting an array element with a sequence usually means NumPy expected one scalar value, but your code gave it a list, tuple, array, or another sequence. The most common causes are ragged nested lists, assigning a list into one numeric array cell, or forcing data into a dtype that cannot represent it.
Start by checking the shape and dtype you intended. NumPy arrays are designed for rectangular, mostly homogeneous data. If your rows have different lengths, decide whether to pad them, keep them as Python lists, or explicitly create an object array.
Quick fixes
| Cause | Fix |
|---|---|
| Rows have different lengths | Make every row the same length, or use dtype=object only if ragged data is intentional |
| Assigning a list to one scalar cell | Assign to a slice, reshape the array, or store objects intentionally |
| Forcing incompatible dtype | Clean the data before conversion or choose the correct dtype |
| Pandas or ML input is nested inconsistently | Normalize column values before converting to NumPy arrays |
Cause 1: Ragged nested lists
A two-dimensional NumPy array must have a rectangular shape. This input is ragged because the first row has three values and the second row has two:
import numpy as np
rows = [[1, 2, 3], [4, 5]]
arr = np.array(rows)
Modern NumPy versions reject this because there is no clean rectangular numeric array to create.
Fix it by making each row the same length:
rows = [[1, 2, 3], [4, 5, 0]]
arr = np.array(rows)
print(arr.shape)
(2, 3)
If the missing value is meaningful, use a sentinel such as 0, np.nan, or a mask depending on the problem.
Use dtype=object only when ragged data is intentional
If you truly want an array whose elements are Python lists, say so explicitly:
rows = [[1, 2, 3], [4, 5]]
arr = np.array(rows, dtype=object)
print(arr.dtype)
object
This avoids the rectangular-array error, but it also changes what you have. An object array is not the same thing as a normal numeric matrix. Many NumPy operations, Pandas conversions, and machine-learning estimators expect rectangular numeric data, so prefer padding or cleaning data when you need numeric computation.

Cause 2: Assigning a sequence to one scalar cell
This error can also happen after an array already exists:
arr = np.zeros(3, dtype=int)
arr[0] = [10, 20]
arr[0] is one scalar slot, so NumPy cannot put two values into it. Assign one scalar value:
arr[0] = 10
Or assign a sequence to a slice with the same length:
arr[:2] = [10, 20]
print(arr)
[10 20 0]
If every element should itself contain a sequence, use a Python list of lists or an object array deliberately.
Cause 3: Incompatible dtype conversion
Specifying dtype=int or another strict dtype can expose bad input:
values = [[1, 2], [3, "missing"]]
arr = np.array(values, dtype=int)
Fix the source data before conversion:
values = [[1, 2], [3, 0]]
arr = np.array(values, dtype=int)
For dtype-related NumPy migration issues, see the Python Pool guide to module ‘numpy’ has no attribute ‘bool’.
Pandas version of the error
Pandas often surfaces this error when one column contains lists of different lengths and the code later tries to convert that column to a rectangular NumPy array.
import pandas as pd
import numpy as np
df = pd.DataFrame({"features": [[1, 2, 3], [4, 5]]})
features = np.array(df["features"].tolist())
Normalize the feature vectors first:
features = [[1, 2, 3], [4, 5, 0]]
arr = np.array(features)
When the data is already rectangular, converting between NumPy and Pandas is straightforward. See NumPy array to Pandas DataFrame for that direction.

Machine-learning input arrays
Scikit-learn and many ML libraries expect a two-dimensional numeric feature matrix shaped like (n_samples, n_features). If one sample has fewer or more features than another, normalize the data before fitting the model.
# Good: two samples, three features each
X = np.array([
[1.2, 0.7, 3.4],
[0.9, 1.1, 2.8],
])
For image and plotting pipelines, the related error image data of dtype object cannot be converted to float often has the same root cause: object or ragged data reached code that expected numeric arrays.
How to debug the error
- Print the nested list lengths:
[len(row) for row in rows]. - Print
arr.shapeandarr.dtypebefore the failing line. - Check whether you are assigning to one scalar cell or to a slice.
- Use
dtype=objectonly when you intentionally want Python objects inside the array. - Pad, trim, or filter data before converting to a numeric array.

Official references
The NumPy documentation explains that arrays are usually rectangular and homogeneous in the NumPy basics guide. It also documents numpy.array(), ndarray, dtype objects, and numpy.pad(). NumPy’s NEP 34 explains why ragged nested sequences must be explicit with dtype=object.
Conclusion
To fix ValueError: setting an array element with a sequence, make sure the target location expects a sequence, not a scalar, and make sure your nested data has a rectangular shape when you want a numeric NumPy array. Use dtype=object only for intentionally ragged data, not as a quick fix for data that should be cleaned first.
Read The Shape Contract
A numeric array has a rectangular shape. If one row contains more values than another, or a scalar target receives a list, NumPy cannot infer the intended dimensions for a regular numeric dtype.
Inspect Before Construction
Check type, shape where available, row lengths, nesting depth, and dtype immediately before np.array or assignment. A short diagnostic often reveals that one record has a different schema.
Normalize Ragged Input
Pad, trim, reject, or transform rows according to the application contract. Use an object array only when variable-length values are truly intended and downstream code handles them explicitly.

Separate Scalar Assignment
When assigning to arr[i, j], provide one compatible scalar. If the desired value is a vector, select a slice with the matching shape instead of a single element.
Validate At Boundaries
Data from CSV, JSON, forms, and APIs should be checked before numerical operations. Preserve the original record identifier so a malformed row can be corrected rather than silently discarded.
Test Irregular Cases
Test rectangular data, empty rows, ragged rows, nested lists, scalar and vector assignments, object dtype, mixed types, and clear error messages. Assert shape and dtype after normalization.
The official NumPy broadcasting guide explains compatible shapes. Related Python Pool references include NumPy arrays and tests.
For related array debugging, compare NumPy shape handling, input validation tests, and nested sequences before assigning values.
Frequently Asked Questions
What causes setting an array element with a sequence?
A scalar array position received a list or array, or nested input rows have inconsistent lengths for the requested numeric shape.
How do I fix ragged NumPy input?
Normalize rows to a common length, store them as an object array only when that is intentional, or use a list of arrays with explicit handling.
Why does dtype affect this error?
A numeric dtype expects compatible scalar values; nested sequences cannot be placed into one numeric scalar slot without a shape decision.
How can I debug the failing assignment?
Print or inspect the input type, shape, nesting depth, target index, and dtype immediately before constructing or assigning the array.