diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 5868bdaa1225b..c2bf139bc7e78 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -304,6 +304,8 @@ def array( raise ValueError(msg) elif isinstance(data, ABCDataFrame): raise TypeError("Cannot pass DataFrame to 'pandas.array'") + elif isinstance(data, np.ndarray) and data.ndim != 1 and dtype == object: + raise ValueError("NumpyExtensionArray must be 1-dimensional.") if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ExtensionArray)): # Note: we exclude np.ndarray here, will do type inference on it diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index cd78dfd6f343a..29cfb1522cbf6 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -451,10 +451,16 @@ def test_array_inference_fails(data): tm.assert_extension_array_equal(result, expected) -@pytest.mark.parametrize("data", [np.array(0)]) -def test_nd_raises(data): +@pytest.mark.parametrize( + "data,dtype", + [ + (np.array(0), "int64"), + (np.array([[1, 2], ["a", "b"]]), object), + ], +) +def test_nd_raises(data, dtype): with pytest.raises(ValueError, match="NumpyExtensionArray must be 1-dimensional"): - pd.array(data, dtype="int64") + pd.array(data, dtype=dtype) def test_scalar_raises():