Skip to content

fix: support 0D MeanView/WeightedMeanView#1100

Merged
henryiii merged 2 commits intodevelopfrom
copilot/fix-mean-of-0d-profile
Mar 27, 2026
Merged

fix: support 0D MeanView/WeightedMeanView#1100
henryiii merged 2 commits intodevelopfrom
copilot/fix-mean-of-0d-profile

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 27, 2026

Projecting all axes from a profile histogram yields a 0-d histogram, but accessing .values(), .variances(), .counts(), or any named field on the resulting view raised TypeError: iteration over a 0-d array.

h = bh.Histogram(bh.axis.Regular(10, 0, 100), storage=bh.storage.Mean())
h.fill(x, sample=y)

profile = h.project()          # 0-d Mean histogram
profile.values()               # TypeError: iteration over a 0-d array
profile.view()["value"]        # TypeError: iteration over a 0-d array
profile.view().value           # TypeError: iteration over a 0-d array

Root cause

In View.__getitem__, when a named field is accessed on a 0-d structured array (e.g. view["value"]), numpy returns a 0-d non-structured float array. The old code hit if not sliced.shape first and tried _PARENT._make(*sliced) — unpacking a 0-d array — before it could reach the dtype-changed guard.

Changes

  • src/boost_histogram/view.py — swap check order in View.__getitem__: test sliced.dtype != self.dtype (field access) before not sliced.shape (scalar structured element). The shape check still correctly handles 0-d numpy void scalars produced by integer indexing on a 1-d view.
  • tests/test_views.py — add test_0d_mean_view and test_0d_weighted_mean_view covering .values(), .variances(), .counts(), and direct field access on 0-d profiles.

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copilot AI linked an issue Mar 27, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix issue retrieving mean from 0D profile Fix TypeError when accessing fields of a 0-d MeanView/WeightedMeanView Mar 27, 2026
Copilot AI requested a review from henryiii March 27, 2026 14:42
@github-actions github-actions bot added the needs changelog Might need a changelog entry label Mar 27, 2026
@henryiii henryiii changed the title Fix TypeError when accessing fields of a 0-d MeanView/WeightedMeanView fix: Support 0D MeanView/WeightedMeanView Mar 27, 2026
@henryiii henryiii changed the title fix: Support 0D MeanView/WeightedMeanView fix: support 0D MeanView/WeightedMeanView Mar 27, 2026
@henryiii henryiii marked this pull request as ready for review March 27, 2026 15:07
@henryiii henryiii merged commit 6881838 into develop Mar 27, 2026
20 checks passed
@henryiii henryiii deleted the copilot/fix-mean-of-0d-profile branch March 27, 2026 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Might need a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Cannot get mean of 0D profile

2 participants