Skip to content

BUG: pass 2D ndarray and EA-dtype to DataFrame, closes #12513 #30507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 1, 2020
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,8 @@ Other
- Fixed ``pow`` operations for :class:`IntegerArray` when the other value is ``0`` or ``1`` (:issue:`29997`)
- Bug in :meth:`Series.count` raises if use_inf_as_na is enabled (:issue:`29478`)
- Bug in :class:`Index` where a non-hashable name could be set without raising ``TypeError`` (:issue:`29069`)

- Bug in :class:`DataFrame` constructor when passing a 2D ``ndarray`` and an extension dtype (:issue:`12513`)
-

.. _whatsnew_1000.contributors:

Expand Down
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)