Skip to content

fix: dtype handling and axis validation in statistical functions#145

Draft
henryiii wants to merge 1 commit into
mainfrom
fix/statistical-dtype-and-axis
Draft

fix: dtype handling and axis validation in statistical functions#145
henryiii wants to merge 1 commit into
mainfrom
fix/statistical-dtype-and-axis

Conversation

@henryiii

Copy link
Copy Markdown
Member

🤖 AI text below 🤖

Fixes three confirmed bugs in src/ragged/_spec_statistical_functions.py.

1. sum / prod ignored their dtype argument

The cast condition was inverted: it cast only when the dtypes already
matched (a no-op) and skipped the cast when they actually differed.

  • Before: ragged.sum(ragged.array([[1, 2, 3], [4, 5]]), dtype=np.float32) returned int64.
  • After: returns float32 (value 15.0). The cast now happens via the
    existing _ensure_dtype helper, which also handles the 0-d scalar case.

2. mean / var / std truncated integer input

These functions cast their floating-point result back to x.dtype,
truncating integer input.

  • Before: ragged.mean(ragged.array([1, 2])) returned 1;
    ragged.var(ragged.array([1, 2, 4])) returned 1 (true ≈ 1.556).
  • After: integer/bool input keeps its natural floating-point dtype
    (mean([1, 2]) == 1.5), matching the Array API requirement that these
    functions return a floating-point data type. Floating input (e.g.
    float32) is still cast back to preserve the "same data type as x"
    promise. Docstrings' "Returns" wording updated accordingly. std
    delegates to var, so it follows automatically.

3. _regularize_axis built an error message but never raised it

For tuple axes, the out-of-bounds branch assigned msg but never raised,
and the bound check 0 < out[-1] incorrectly rejected axis 0.

  • Before: ragged.sum(x, axis=(0, 5)) fell through to a less helpful
    awkward error.
  • After: raises ak.errors.AxisError with a clear message; the bound
    is now 0 <= out[-1] < ndim, so tuple axes including 0 work.

Tests

Added regression tests in tests/test_spec_statistical_functions.py
covering all three fixes. Full suite passes (1305 passed, 6 skipped),
mypy --strict and all pre-commit hooks clean.

Part of #144

🤖 Generated with Claude Code

sum and prod ignored their dtype argument due to an inverted condition
(cast only when dtypes already matched). They now cast via _ensure_dtype
whenever the requested dtype differs.

mean, var, and std cast their floating-point result back to x.dtype,
truncating integer input (e.g. mean([1, 2]) returned 1). They now keep
the natural floating-point dtype for integer/bool input while still
preserving float32 input dtype.

_regularize_axis built an out-of-bounds error message for tuple axes but
never raised it, and used an incorrect 0 < out[-1] bound that rejected
axis 0. It now raises ak.errors.AxisError and uses 0 <= out[-1].

Part of #144

Assisted-by: ClaudeCode:claude-opus-4-8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant