Skip to content

Commit cb7b294

Browse files
authored
BUG: Create empty dataframe with string dtype fails (#33651)
1 parent 3ac34ff commit cb7b294

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ ExtensionArray
766766
- Fixed bug that caused :meth:`Series.__repr__()` to crash for extension types whose elements are multidimensional arrays (:issue:`33770`).
767767
- Fixed bug where :meth:`Series.update` would raise a ``ValueError`` for ``ExtensionArray`` dtypes with missing values (:issue:`33980`)
768768
- Fixed bug where :meth:`StringArray.memory_usage` was not implemented (:issue:`33963`)
769+
- Fixed bug that `DataFrame(columns=.., dtype='string')` would fail (:issue:`27953`, :issue:`33623`)
769770

770771

771772
Other

pandas/core/internals/construction.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ def init_dict(data: Dict, index, columns, dtype: Optional[DtypeObj] = None):
257257

258258
# no obvious "empty" int column
259259
if missing.any() and not is_integer_dtype(dtype):
260-
if dtype is None or np.issubdtype(dtype, np.flexible):
260+
if dtype is None or (
261+
not is_extension_array_dtype(dtype)
262+
and np.issubdtype(dtype, np.flexible)
263+
):
261264
# GH#1783
262265
nan_dtype = np.dtype(object)
263266
else:

pandas/tests/extension/arrow/test_bool.py

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def test_series_constructor_scalar_na_with_index(self, dtype, na_value):
6969
# pyarrow.lib.ArrowInvalid: only handle 1-dimensional arrays
7070
super().test_series_constructor_scalar_na_with_index(dtype, na_value)
7171

72+
@pytest.mark.xfail(reason="raises AssertionError")
73+
def test_construct_empty_dataframe(self, dtype):
74+
super().test_construct_empty_dataframe(dtype)
75+
7276

7377
class TestReduce(base.BaseNoReduceTests):
7478
def test_reduce_series_boolean(self):

pandas/tests/extension/base/constructors.py

+8
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,11 @@ def test_pandas_array_dtype(self, data):
108108
result = pd.array(data, dtype=np.dtype(object))
109109
expected = pd.arrays.PandasArray(np.asarray(data, dtype=object))
110110
self.assert_equal(result, expected)
111+
112+
def test_construct_empty_dataframe(self, dtype):
113+
# GH 33623
114+
result = pd.DataFrame(columns=["a"], dtype=dtype)
115+
expected = pd.DataFrame(
116+
{"a": pd.array([], dtype=dtype)}, index=pd.Index([], dtype="object")
117+
)
118+
self.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)