Skip to content

Commit

Permalink
Add _get_column_of_values() util fn
Browse files Browse the repository at this point in the history
  • Loading branch information
rich-iannone committed Oct 24, 2023
1 parent 98b2e0e commit e954c9a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
16 changes: 15 additions & 1 deletion gt/gt.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,24 @@ def _set_has_built(gt: GT, value: bool) -> GT:

def _get_column_labels(gt: GT, context: str) -> List[str]:
gt_built = gt._build_data(context=context)
column_labels = gt_built._boxhead._get_column_labels()
# column_labels = gt_built._boxhead._get_column_labels()
column_labels = [x.column_label for x in gt_built._boxhead._boxhead]
return column_labels


def _get_column_of_values(gt: GT, column_name: str, context: str) -> List[str]:
gt_built = gt._build_data(context=context)
tbl_data = gt_built._body.body
cell_values: List[str] = []

for i in range(n_rows(tbl_data)):
cell_content: Any = _get_cell(tbl_data, i, column_name)
cell_str: str = str(cell_content)
cell_values.append(cell_str)

return cell_values


# =============================================================================
# Table Structuring Functions
# =============================================================================
Expand Down
23 changes: 16 additions & 7 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import pandas as pd
import pytest
from pandas.testing import assert_frame_equal

from gt import GT
from gt.gt import _get_column_of_values


@pytest.mark.xfail(reason="Don't have a method to get formatted values as list")
def test_fmt_number_basic():
df = pd.DataFrame({"x": [1.234, 2.345], "y": [3.456, 4.567]})

# Expect that values in `x` are formatted to 2 decimal places
gt = GT(df).fmt_number(columns="x", decimals=2)
x = _get_column_of_values(gt, column_name="x", context="html")
y = ["1.23", "2.35"]
assert x == y

# TODO: is 2.35 below the intended result?
res = gt._build_data("html")._body.body
dst = df.assign(x=["1.23", "2.35"])
# TODO: this fails because unformatted values not migrated to body
# Expect that values in `y` are formatted to 2 decimal places
# x = _get_column_of_values(gt, column_name="y", context="html")
# y = ["3.46", "4.57"] # is currently ['<NA>', '<NA>']
# assert x == y

assert_frame_equal(res, dst)
# Expect that values in `x` are formatted to 5 decimal places
gt = GT(df).fmt_number(columns="x", decimals=5)
x = _get_column_of_values(gt, column_name="x", context="html")
y = ["1.23400", "2.34500"]
assert x == y

0 comments on commit e954c9a

Please sign in to comment.