BUG: inferred COO reshape dimensions lose integer precision

Open
#1,004 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@nawaaaaaAaar is already working on this.

Since Oct 22, 2026.

  • #1009 by @nawaaaaaAaar — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
numpy, python
Domain
data

Research direction

Start from the COO reshape path that infers a dimension from -1, then trace how it calculates the inferred size. Reproduce the large sparse shape case and the multiple-unknown-dimensions case described in the issue, and compare their results with NumPy. Done means both cases match the expected behavior and the four regression cases pass; an open linked pull request already addresses this work.

Written by the indexing model from the issue text.

Description

Bug

COO inferred reshapes pass the dimension calculation through floating-point division. This loses integer precision for large but extremely sparse shapes.

Reproduced on main at d63255057744331b96626a1f1c24504b3a3783cd.

import numpy as np
import sparse

rows = 2**53 + 1
size = 3 * rows
x = sparse.COO(np.array([[size - 1]], dtype=np.int64), [7], shape=(size,))
x.reshape((-1, 3))
# ValueError, although size is exactly divisible by 3.

The expected shape is (9007199254740993, 3) and the single stored coordinate should be (9007199254740992, 2). This reproducer stores only one value; it does not allocate a huge dense array.

The same inferred-dimension path also silently accepts multiple -1 entries when the array has size one, unlike NumPy.

A focused fix using exact integer division and rejecting multiple unknown dimensions is ready with regression tests. Four new cases fail before the production change.

This is an AI-assisted bug investigation with locally executed reproductions and tests. No assignment is requested.

Dominant language
Python
Stars
670
Forks
167
Avg merge
3d 4h
Merged PRs (30d)
7

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydata/sparse

All issues in pydata/sparse

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.