Skip to content

Error when using hierarchical columns with outer values ungrouped #4

@ForeverWintr

Description

@ForeverWintr

A dataframe with hierarchical columns where the outer labels are not grouped causes the an error in to_xlsx_worksheet. I think it's most easily illustrated with an example:

from table_compositor.table_compositor import build_presentation_model
from table_compositor import xlsx_writer
import static_frame as sf

cols = sf.IndexHierarchy.from_labels([(1, 1), (2, 2), (1, 2)])
frame = sf.Frame.from_element(1, columns=cols, index=[1, 2, 3])
df = frame.to_pandas()

# df:
# __index0__  1  2  1
# __index1__  1  2  2
# 1           1  1  1
# 2           1  1  1
# 3           1  1  1

pm = build_presentation_model(df=df)

# I'm setting worksheet to None because the error occurs regardless of whether it's provided.
xlsx_writer.OpenPyxlCompositor.to_xlsx_worksheet(layout=[pm], worksheet=None)

This produces the following traceback:

  File "/home/rutherford/.env38/lib/python3.8/site-packages/table_compositor/xlsx_writer.py", line 128, in to_xlsx_worksheet
    row_col_dict = cls._build_row_col_dict(
  File "/home/rutherford/.env38/lib/python3.8/site-packages/table_compositor/xlsx_writer.py", line 27, in _build_row_col_dict
    row_col_dict = GridLayoutManager.get_row_col_dict(
  File "/home/rutherford/.env38/lib/python3.8/site-packages/table_compositor/grid.py", line 135, in get_row_col_dict
    return GridLayoutManager.foldl(shifted_grid, f, dict())
  File "/home/rutherford/.env38/lib/python3.8/site-packages/table_compositor/grid.py", line 53, in foldl
    accum = GridLayoutManager.foldl(child, f, accum)
  File "/home/rutherford/.env38/lib/python3.8/site-packages/table_compositor/grid.py", line 45, in foldl
    accum = f(accum, cell.children)
  File "/home/rutherford/.env38/lib/python3.8/site-packages/table_compositor/grid.py", line 134, in <lambda>
    f = lambda accum, pm: {**accum, **to_row_col_dict(pm)}
  File "/home/rutherford/.env38/lib/python3.8/site-packages/table_compositor/presentation_model.py", line 147, in to_row_col_dict
    data = {
  File "/home/rutherford/.env38/lib/python3.8/site-packages/table_compositor/presentation_model.py", line 149, in <dictcomp>
    for offsets, value, style in data
builtins.ValueError: too many values to unpack (expected 3)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions