Skip to content

Commit

Permalink
fix bug in dataframe.from_records with str index and empty iterable i…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelvingandhi committed Jun 12, 2022
1 parent a45c7fa commit 8c273a1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ Conversion
Strings
^^^^^^^
- Bug in :meth:`str.startswith` and :meth:`str.endswith` when using other series as parameter _pat_. Now raises ``TypeError`` (:issue:`3485`)
-
- Bug in :meth:`DataFrame.from_records` raises a ``KeyError`` if passed a string index and an empty iterable (:isue:`#47285`)

Interval
^^^^^^^^
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,13 @@ def maybe_reorder(
arr_columns = ensure_index(arr_columns)
if columns is None:
columns = arr_columns

if index is not None:
if isinstance(index, str) or not hasattr(index, "__iter__"):
if index not in columns:
result_index = [index]
index = None

else:
arrays, arr_columns, result_index = maybe_reorder(
arrays, arr_columns, columns, index
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/formats/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,10 @@ def test_info_int_columns():
"""
)
assert result == expected

def test_info_from_rec():
# GH47285
df = DataFrame.from_records([], index="foo")
print(df.info())
#buf = StringIO()
#df.info(buf=buf)

0 comments on commit 8c273a1

Please sign in to comment.