Skip to content

BUG: fix asarray failure for object dtype with matching leading dimensions#30831

Closed
Sarthak7Gautam wants to merge 1 commit intonumpy:mainfrom
Sarthak7Gautam:numpy-bug-fix
Closed

BUG: fix asarray failure for object dtype with matching leading dimensions#30831
Sarthak7Gautam wants to merge 1 commit intonumpy:mainfrom
Sarthak7Gautam:numpy-bug-fix

Conversation

@Sarthak7Gautam
Copy link
Copy Markdown

Summary

This PR fixes a ValueError in np.asarray when providing a list of arrays that share a common leading dimension but have different subsequent dimensions when dtype=object is specified.

The Problem

Currently, np.asarray([a, b], dtype=object) fails if a.shape is (10, 10) and b.shape is (10, 9). NumPy incorrectly attempts to broadcast these into a higher-dimensional array because the first dimensions match, rather than falling back to a 1D object array.

Example of the failure:

import numpy as np
a = np.ones((10, 10))
b = np.ones((10, 9))
# This currently raises ValueError
np.asarray([a, b], dtype=object)

I have updated the array creation logic in [main.py , numpy/core/src/multiarray/methods.c] to ensure that when dtype=object is requested, NumPy prioritizes creating a ragged object array if subsequent dimensions do not match.
Fixes #30830

…py thought it would be a 3d shape so tried to broadcast it but when 10,9 was encountered as different shapes it failed so transpose to make numpy know that you want to create a 2d array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants