Repository navigation
BUG: Make array_like return a base-class ndarray, clearer dimension message - #11242
Merged
Merged
Conversation
`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).
This was referenced Nov 26, 2026
bashtage
marked this pull request as ready for review
November 26, 2026 21:00
4 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BUG: Make array_like return a base-class ndarray
array_likeis documented to return an ndarray, and statsmodels uses itwhere
np.asarraywould otherwise be called. It passednp.requireno"E" requirement, so subclasses of ndarray were returned unchanged: an
np.matrixkept matrix semantics (2-d always,*is matrixmultiplication, 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.requirefor a base-class ndarray, asnp.asarrayreturns.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.asarraychecks be replaced byarray_likewithout changing what happens to these inputs.
Tests: a matrix, a masked array and another ndarray subclass give a
base-class ndarray equal to
np.asarrayof the input, with and withoutwriteable, and a matrix column is squeezed to 1-d withndim=1. Theyfail on main.
ENH: Say how many dimensions array_like expected and got
When
shapehas a different number of entries than the input hasdimensions,
array_likeraised "Provided shape (None, None) does not havethe 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).
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.
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