From 88170707fd7dca54bd2b0af687f718dee51ae772 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 15 Dec 2019 23:45:12 +0000 Subject: [PATCH] BUG: DataFrame constructor raised ValueError with list-like data and dtype specified --- doc/source/whatsnew/v1.0.0.rst | 2 +- pandas/core/internals/construction.py | 2 +- pandas/tests/extension/base/constructors.py | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 539649df05046..7982dd70178e9 100755 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -861,7 +861,7 @@ ExtensionArray - Bug in :class:`arrays.PandasArray` when setting a scalar string (:issue:`28118`, :issue:`28150`). - Bug where nullable integers could not be compared to strings (:issue:`28930`) -- +- Bug where :class:`DataFrame` constructor raised ValueError with list-like data and ``dtype`` specified (:issue:`30280`) Other diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index ae1f37aa5cd45..cee196c94dacf 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -150,7 +150,7 @@ def init_ndarray(values, index, columns, dtype=None, copy=False): index, columns = _get_axes(len(values), 1, index, columns) return arrays_to_mgr([values], columns, index, columns, dtype=dtype) - elif is_extension_array_dtype(values): + elif is_extension_array_dtype(values) or is_extension_array_dtype(dtype): # GH#19157 if columns is None: columns = [0] diff --git a/pandas/tests/extension/base/constructors.py b/pandas/tests/extension/base/constructors.py index 7262a85b1fe00..c40646ca2415e 100644 --- a/pandas/tests/extension/base/constructors.py +++ b/pandas/tests/extension/base/constructors.py @@ -64,6 +64,15 @@ def test_from_dtype(self, data): result = pd.Series(list(data), dtype=str(dtype)) self.assert_series_equal(result, expected) + # gh-30280 + + expected = pd.DataFrame(data).astype(dtype) + result = pd.DataFrame(list(data), dtype=dtype) + self.assert_frame_equal(result, expected) + + result = pd.DataFrame(list(data), dtype=str(dtype)) + self.assert_frame_equal(result, expected) + def test_pandas_array(self, data): # pd.array(extension_array) should be idempotent... result = pd.array(data)