Skip to content
Merged
12 changes: 10 additions & 2 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,17 @@ def init_ndarray(values, index, columns, dtype=None, copy=False):
return arrays_to_mgr([values], columns, index, columns, dtype=dtype)
elif is_extension_array_dtype(values) or is_extension_array_dtype(dtype):
# GH#19157

if isinstance(values, np.ndarray) and values.ndim > 1:
# GH#12513 a EA dtype passed with a 2D array, split into
# multiple EAs that view the values
values = [values[:, n] for n in range(values.shape[1])]
else:
values = [values]

if columns is None:
columns = [0]
return arrays_to_mgr([values], columns, index, columns, dtype=dtype)
columns = list(range(len(values)))
return arrays_to_mgr(values, columns, index, columns, dtype=dtype)

# by definition an array here
# the dtypes will be coerced to a single dtype
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2551,3 +2551,11 @@ def test_from_tzaware_mixed_object_array(self):
"datetime64[ns, CET]",
]
assert (res.dtypes == expected_dtypes).all()

def test_from_2d_ndarray_with_dtype(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this test more generic and add to the EA tests instead (followup ok)

# GH#12513
array_dim2 = np.arange(10).reshape((5, 2))
df = pd.DataFrame(array_dim2, dtype="datetime64[ns, UTC]")

expected = pd.DataFrame(array_dim2).astype("datetime64[ns, UTC]")
tm.assert_frame_equal(df, expected)