diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index cc7706741e653..c987b39130095 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -546,6 +546,7 @@ Interval Indexing ^^^^^^^^ - Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`) +- Bug in :meth:`DataFrame.from_records` throwing a ``ValueError`` when passed an empty list in ``index`` (:issue:`58594`) - Missing diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 7d43498d4267b..5bffac5fa64b6 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -7473,9 +7473,12 @@ def ensure_index_from_sequences(sequences, names=None) -> Index: -------- ensure_index """ + from pandas.core.indexes.api import default_index from pandas.core.indexes.multi import MultiIndex - if len(sequences) == 1: + if len(sequences) == 0: + return default_index(0) + elif len(sequences) == 1: if names is not None: names = names[0] return Index(maybe_sequence_to_range(sequences[0]), name=names) diff --git a/pandas/tests/frame/constructors/test_from_records.py b/pandas/tests/frame/constructors/test_from_records.py index 5be42d41af03a..ed2f0aa9c4679 100644 --- a/pandas/tests/frame/constructors/test_from_records.py +++ b/pandas/tests/frame/constructors/test_from_records.py @@ -148,6 +148,12 @@ def test_from_records_sequencelike_empty(self): assert len(result) == 0 assert len(result.columns) == 0 + def test_from_records_sequencelike_empty_index(self): + result = DataFrame.from_records([], index=[]) + assert len(result) == 0 + assert len(result.columns) == 0 + assert len(result.index) == 0 + def test_from_records_dictlike(self): # test the dict methods df = DataFrame(