Skip to content

Commit

Permalink
BUG: pass 2D ndarray and EA-dtype to DataFrame, closes #12513 (#30507)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Jan 1, 2020
1 parent efaadd5 commit 765d8db
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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 @@ -983,7 +983,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):
# 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)

0 comments on commit 765d8db

Please sign in to comment.