django/django · error · TypeError
Must set coordinate with a sequence (list, tuple, or numpy a
Error message
Must set coordinate with a sequence (list, tuple, or numpy array).
What it means
Raised in GEOSCoordSeq.__setitem__ when the value being assigned to a coordinate index is not a list, tuple, or numpy.ndarray. Coordinate slots must receive a sequence of ordinates (e.g. (x,y) or [x,y,z]) so the setter can unpack them; scalars, strings, or generators are rejected. numpy is optional, so ndarray is only accepted if numpy is installed.
Source
Thrown at django/contrib/gis/geos/coordseq.py:56
def __str__(self):
"Return the string representation of the coordinate sequence."
return str(self.tuple)
def __getitem__(self, index):
"Return the coordinate sequence value at the given index."
self._checkindex(index)
return self._point_getter(index)
def __setitem__(self, index, value):
"Set the coordinate sequence value at the given index."
# Checking the input value
if isinstance(value, (list, tuple)):
pass
elif numpy and isinstance(value, numpy.ndarray):
pass
else:
raise TypeError(
"Must set coordinate with a sequence (list, tuple, or numpy array)."
)
# Checking the dims of the input
if self.dims == 3 and self._z:
n_args = 3
point_setter = self._set_point_3d
elif self.dims == 3 and self.hasm:
n_args = 3
point_setter = self._set_point_3d_m
elif self.dims == 4 and self._z and self.hasm:
n_args = 4
point_setter = self._set_point_4d
else:
n_args = 2
point_setter = self._set_point_2d
if len(value) != n_args:
raise TypeError("Dimension of value does not match.")
self._checkindex(index)View on GitHub (pinned to ae25a40be0)
Solutions
- Assign a list/tuple: seq[i] = (x, y) or [x, y, z]
- For numpy users: seq[i] = np.array([x, y])
- Use setOrdinate(dim, index, value) to set a single ordinate instead
Example fix
// before seq[0] = 5.0 seq[0] = '10 20' // after seq[0] = (10, 20) # or single ordinate seq.setX(0, 10)
Defensive patterns
Strategy: type-guard
Validate before calling
from django.contrib.gis.shortcuts import numpy
def coerce_coord_value(v):
if isinstance(v, (list, tuple)):
return v
if numpy and isinstance(v, numpy.ndarray):
return v
raise TypeError('coordinate must be list/tuple/ndarray') Type guard
from django.contrib.gis.shortcuts import numpy
def is_coord_sequence(v) -> bool:
return isinstance(v, (list, tuple)) or (numpy is not None and isinstance(v, numpy.ndarray)) Try / catch
try:
seq[i] = value
except TypeError as e:
if 'Must set coordinate' in str(e):
seq[i] = tuple(value)
raise Prevention
- Always assign (x, y) or [x, y, z]
- Use setX/setY/setZ for single ordinates
- Avoid passing parsed strings without splitting
When it happens
Trigger: Doing seq[i] = 5.0 (scalar), seq[i] = '5,6' (string), seq[i] = generator, or seq[i] = some_object. Also triggered when assigning a single ordinate where a tuple was expected.
Common situations: Treating a coordinate slot as a scalar field; passing coordinates parsed from a CSV string without splitting; forgetting to wrap lon/lat in a tuple.
Related errors
- Coordinate sequence should initialize with a CS_PTR.
- Dimension of value does not match.
- Invalid GEOS Geometry index: {index}
- Invalid data source input type: "{}".
- Raster needs to be opened in write mode to change values.
AI-assisted analysis of django/django@ae25a40be0 (2026-08-06).
Data as JSON: /api/errors/45eed89c7e599347.
Report an issue: GitHub.