Skip to content

Commit 8c273a1

Browse files
committed
fix bug in dataframe.from_records with str index and empty iterable issue pandas-dev#47285
1 parent a45c7fa commit 8c273a1

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ Conversion
776776
Strings
777777
^^^^^^^
778778
- Bug in :meth:`str.startswith` and :meth:`str.endswith` when using other series as parameter _pat_. Now raises ``TypeError`` (:issue:`3485`)
779-
-
779+
- Bug in :meth:`DataFrame.from_records` raises a ``KeyError`` if passed a string index and an empty iterable (:isue:`#47285`)
780780

781781
Interval
782782
^^^^^^^^

pandas/core/frame.py

+7
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,13 @@ def maybe_reorder(
22912291
arr_columns = ensure_index(arr_columns)
22922292
if columns is None:
22932293
columns = arr_columns
2294+
2295+
if index is not None:
2296+
if isinstance(index, str) or not hasattr(index, "__iter__"):
2297+
if index not in columns:
2298+
result_index = [index]
2299+
index = None
2300+
22942301
else:
22952302
arrays, arr_columns, result_index = maybe_reorder(
22962303
arrays, arr_columns, columns, index

pandas/tests/io/formats/test_info.py

+7
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,10 @@ def test_info_int_columns():
491491
"""
492492
)
493493
assert result == expected
494+
495+
def test_info_from_rec():
496+
# GH47285
497+
df = DataFrame.from_records([], index="foo")
498+
print(df.info())
499+
#buf = StringIO()
500+
#df.info(buf=buf)

0 commit comments

Comments
 (0)