From 004570f7bcff326f4150ede19831d00be5cf8f25 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 26 Dec 2019 19:26:54 -0800 Subject: [PATCH 1/2] BUG: pass 2D ndarray and EA-dtype to DataFrame, closes #12513 --- pandas/core/internals/construction.py | 12 ++++++++++-- pandas/tests/frame/test_constructors.py | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 897dbe2e8f788..3a92cfd9bf16d 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -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 diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index f3cc11cb7027d..ffdf1435f74e0 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -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) From 2b1c51054b38dcf3b2c9d3594514a1764e1b30ce Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 27 Dec 2019 09:15:31 -0800 Subject: [PATCH 2/2] whatsnew --- doc/source/whatsnew/v1.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 7948b3bf2fd2f..cc4473dfd87bc 100755 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -902,6 +902,7 @@ 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: