Skip to content

Commit

Permalink
[pandas] handle read methods that produce a list of dataframes
Browse files Browse the repository at this point in the history
Pandas read methods (like `read_html`) can return a list of dataframes
rather than a single one. In that case, use the first element of the
list for the current sheet, and push additional dataframes to new
sheets.
  • Loading branch information
ajkerrigan authored and saulpw committed Aug 8, 2023
1 parent 5386bc8 commit 5d11c67
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions visidata/loaders/_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ def reload(self):
readfunc = getattr(pd, 'read_'+filetype) or vd.error('no pandas.read_'+filetype)
# readfunc() handles binary and text open()
df = readfunc(self.source, **options.getall('pandas_'+filetype+'_'))
# some read methods (html, for example) return a list of dataframes
if isinstance(df, list):
for idx, inner_df in enumerate(df[1:], start=1):
vd.push(PandasSheet(f'{self.name}[{idx}]', source=inner_df))
df = df[0]
self.name += '[0]'
if (filetype == 'pickle') and not isinstance(df, pd.DataFrame):
vd.fail('pandas loader can only unpickle dataframes')
else:
Expand Down

0 comments on commit 5d11c67

Please sign in to comment.