Skip to content

Commit fd74f78

Browse files
ankaneChristopher Krah
andcommitted
Fixed error with unordered SciPy sparse arrays - resolves #160
Co-authored-by: Christopher Krah <chris@plexito.de>
1 parent 99a6776 commit fd74f78

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 0.5.1 (unreleased)
22

3+
- Fixed error with unordered SciPy sparse arrays
34
- Fixed error with `Bit` constructor for NumPy 1.24-1.26
45

56
## 0.5.0 (2026-07-06)

‎pgvector/sparsevec.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,14 @@ def _from_sparse(self, arr: sparray | spmatrix, /) -> None:
115115

116116
if hasattr(value, 'coords'):
117117
# scipy 1.13+
118-
self._indices = value.coords[-1].tolist()
118+
indices = value.coords[-1].tolist()
119119
else:
120-
self._indices = value.col.tolist()
121-
self._values = [float(v) for v in value.data]
120+
indices = value.col.tolist()
121+
122+
elements = sorted(zip(indices, value.data))
123+
124+
self._indices = [v[0] for v in elements]
125+
self._values = [float(v[1]) for v in elements]
122126

123127
def _from_dense(self, value: list[float] | ndarray, /) -> None:
124128
self._dim = len(value)

‎tests/test_sparse_vector.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_coo_array(self) -> None:
5454
if np is None or sparse is None:
5555
pytest.skip('NumPy and SciPy required')
5656

57-
arr = sparse.coo_array(np.array([1, 0, 2, 0, 3, 0]))
57+
arr = sparse.coo_array(([3, 1, 2], ([4, 0, 2],)), shape=(6,))
5858
vec = SparseVector(arr)
5959
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
6060
assert vec.indices() == [0, 2, 4]

0 commit comments

Comments
 (0)