diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 7f3d246a6fda6..1a85dbe70a5e3 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -456,6 +456,17 @@ def dict_to_mgr( ] # TODO: can we get rid of the dt64tz special case above? + if dtype is None and index is None: + data_len = 0 + for each in data.values(): + if isinstance(each, (int, str)): + data_len += 1 + elif each is None: + continue + else: + data_len += len(each) + if data_len == 0: + dtype = object return arrays_to_mgr( arrays, data_names, index, columns, dtype=dtype, typ=typ, consolidate=copy ) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index abb29ce66fd34..414f81fdd5a2d 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -1095,3 +1095,10 @@ def test_period_dtype_compare_to_string(): dtype = PeriodDtype(freq="M") assert (dtype == "period[M]") is True assert (dtype != "period[M]") is False + + +def test_dataframe_constructor_dtype(): + # GH#42971 + expected = pd.DataFrame(columns=["a"]) + result = pd.DataFrame({"a": []}) + tm.assert_series_equal(result.dtypes, expected.dtypes)