diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 816ef4e5c9eb6..3cef9fbe49a9b 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -759,6 +759,7 @@ ExtensionArray - Fixed bug in :class:`Series` construction with EA dtype and index but no data or scalar data fails (:issue:`26469`) - Fixed bug that caused :meth:`Series.__repr__()` to crash for extension types whose elements are multidimensional arrays (:issue:`33770`). - Fixed bug where :meth:`StringArray.memory_usage` was not implemented (:issue:`33963`) +- Fixed bug that `DataFrame(columns=.., dtype='string')` would fail (:issue:`27953`, :issue:`33623`) Other diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index ce3f07d06d6a2..b2af149ccf14c 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -257,7 +257,10 @@ def init_dict(data: Dict, index, columns, dtype: Optional[DtypeObj] = None): # no obvious "empty" int column if missing.any() and not is_integer_dtype(dtype): - if dtype is None or np.issubdtype(dtype, np.flexible): + if dtype is None or ( + not is_extension_array_dtype(dtype) + and np.issubdtype(dtype, np.flexible) + ): # GH#1783 nan_dtype = np.dtype(object) else: diff --git a/pandas/tests/extension/arrow/test_bool.py b/pandas/tests/extension/arrow/test_bool.py index 681c6f9a19dc5..48f1c34764313 100644 --- a/pandas/tests/extension/arrow/test_bool.py +++ b/pandas/tests/extension/arrow/test_bool.py @@ -69,6 +69,10 @@ def test_series_constructor_scalar_na_with_index(self, dtype, na_value): # pyarrow.lib.ArrowInvalid: only handle 1-dimensional arrays super().test_series_constructor_scalar_na_with_index(dtype, na_value) + @pytest.mark.xfail(reason="raises AssertionError") + def test_construct_empty_dataframe(self, dtype): + super().test_construct_empty_dataframe(dtype) + class TestReduce(base.BaseNoReduceTests): def test_reduce_series_boolean(self): diff --git a/pandas/tests/extension/base/constructors.py b/pandas/tests/extension/base/constructors.py index 52e29cffc79c4..5c9e5dcf3ae24 100644 --- a/pandas/tests/extension/base/constructors.py +++ b/pandas/tests/extension/base/constructors.py @@ -108,3 +108,11 @@ def test_pandas_array_dtype(self, data): result = pd.array(data, dtype=np.dtype(object)) expected = pd.arrays.PandasArray(np.asarray(data, dtype=object)) self.assert_equal(result, expected) + + def test_construct_empty_dataframe(self, dtype): + # GH 33623 + result = pd.DataFrame(columns=["a"], dtype=dtype) + expected = pd.DataFrame( + {"a": pd.array([], dtype=dtype)}, index=pd.Index([], dtype="object") + ) + self.assert_frame_equal(result, expected)