Skip to content

BUG: Make array_like return a base-class ndarray, clearer dimension message - #11242

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-array-like-ndarray
Nov 26, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-array-like-ndarray

Conversation

@bashtage

Copy link
Copy Markdown
Member

BUG: Make array_like return a base-class ndarray

array_like is documented to return an ndarray, and statsmodels uses it
where np.asarray would otherwise be called. It passed np.require no
"E" requirement, so subclasses of ndarray were returned unchanged: an
np.matrix kept matrix semantics (2-d always, * is matrix
multiplication, a column cannot be squeezed to 1-d) and a masked array kept
its mask, which later code honoured in some operations and ignored in
others.

It now asks np.require for a base-class ndarray, as np.asarray returns.
A matrix becomes a 2-d ndarray and a masked array its data, with the mask
dropped. Lists, pandas objects and plain ndarrays are unchanged.

This also lets hand-rolled np.asarray checks be replaced by array_like
without changing what happens to these inputs.

Tests: a matrix, a masked array and another ndarray subclass give a
base-class ndarray equal to np.asarray of the input, with and without
writeable, and a matrix column is squeezed to 1-d with ndim=1. They
fail on main.

ENH: Say how many dimensions array_like expected and got

When shape has a different number of entries than the input has
dimensions, array_like raised "Provided shape (None, None) does not have
the correct dimension for data, which is dimension 1". It reported the
dimension after a scalar had been padded to 1-d, so a scalar was "dimension
1". It now says "data must have 2 dimensions, but has 1", counting the
dimensions of the input as given ("but has 0" for a scalar).

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code (Claude Opus 5.5).
    Used for: finding the subclass issue while replacing np.asarray checks with array_like, writing the fix and its tests, and running the full test suite.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

`array_like` is documented to return an ndarray, and statsmodels uses it
where `np.asarray` would otherwise be called. It passed `np.require` no
"E" requirement, so subclasses of ndarray were returned unchanged: an
`np.matrix` kept matrix semantics (2-d always, `*` is matrix
multiplication, a column cannot be squeezed to 1-d) and a masked array kept
its mask, which later code honoured in some operations and ignored in
others.

It now asks `np.require` for a base-class ndarray, as `np.asarray` returns.
A matrix becomes a 2-d ndarray and a masked array its data, with the mask
dropped. Lists, pandas objects and plain ndarrays are unchanged.

This also lets hand-rolled `np.asarray` checks be replaced by `array_like`
without changing what happens to these inputs.

Tests: a matrix, a masked array and another ndarray subclass give a
base-class ndarray equal to `np.asarray` of the input, with and without
`writeable`, and a matrix column is squeezed to 1-d with `ndim=1`. They
fail on main.
When `shape` has a different number of entries than the input has
dimensions, `array_like` raised "Provided shape (None, None) does not have
the correct dimension for data, which is dimension 1". It reported the
dimension after a scalar had been padded to 1-d, so a scalar was "dimension
1". It now says "data must have 2 dimensions, but has 1", counting the
dimensions of the input as given ("but has 0" for a scalar).
@bashtage
bashtage marked this pull request as ready for review November 26, 2026 21:00
@bashtage
bashtage merged commit dfe88fd into statsmodels:main Nov 26, 2026
11 of 12 checks passed
@bashtage
bashtage deleted the wip-array-like-ndarray branch November 27, 2026 10:01
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