Skip to content

Commit

Permalink
Fix todataframe() to do not iterate the table multiple times
Browse files Browse the repository at this point in the history
Converting the table data to a list simply with list() causes the
table to be materialized three times because list() calls __len__()
on the table twice to populate the list.

Anyhow, there is no need to have the table data in a list when an
iteratotor can be passed to pandas.DataFrame.from_records().

Fixes #578.
  • Loading branch information
dnicolodi committed Jan 19, 2022
1 parent 33f7328 commit cbae170
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions petl/io/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def todataframe(table, index=None, exclude=None, columns=None,
"""
import pandas as pd
l = list(table)
data = l[1:]
it = iter(table)
header = next(it)
if columns is None:
columns = l[0]
return pd.DataFrame.from_records(data, index=index, exclude=exclude,
columns = header
return pd.DataFrame.from_records(it, index=index, exclude=exclude,
columns=columns, coerce_float=coerce_float,
nrows=nrows)

Expand Down

0 comments on commit cbae170

Please sign in to comment.