Skip to content

Commit

Permalink
Merge pull request #25 from PumasAI/jk/sort-order-with-missing-groups
Browse files Browse the repository at this point in the history
Sort order in `summarytable` and `listingtable` when groups are missing
  • Loading branch information
jkrumbiegel authored Sep 16, 2024
2 parents 94a257b + 1bbca8a commit b36aa31
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ function _listingtable(
ungroup = false
)

gdf_rows = DataFrames.groupby(df, rowsymbols, sort = false)
gdf_rows = DataFrames.groupby(df, rowsymbols, sort = sort ? (; lt = natural_lt) : false)
row_keys = Tuple.(keys(gdf_rows))
gdf_cols = DataFrames.groupby(df, colsymbols, sort = false)
gdf_cols = DataFrames.groupby(df, colsymbols, sort = sort ? (; lt = natural_lt) : false)
col_keys = Tuple.(keys(gdf_cols))

lt = ListingTable(
Expand Down Expand Up @@ -825,9 +825,9 @@ function _summarytable(
ungroup = false
)

gdf_rows = DataFrames.groupby(_df, rowsymbols, sort = false)
gdf_rows = DataFrames.groupby(_df, rowsymbols; sort = sort ? (; lt = natural_lt) : false)
row_keys = Tuple.(keys(gdf_rows))
gdf_cols = DataFrames.groupby(_df, colsymbols, sort = false)
gdf_cols = DataFrames.groupby(_df, colsymbols; sort = sort ? (; lt = natural_lt) : false)
col_keys = Tuple.(keys(gdf_cols))

st = SummaryTable(
Expand Down
24 changes: 24 additions & 0 deletions test/references/listingtable/missing_groups.latex.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
\documentclass{article}
\usepackage{threeparttable}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{table}[!ht]
\setlength\tabcolsep{0pt}
\centering
\begin{threeparttable}
\begin{tabular}{@{\extracolsep{2ex}}*{4}{lccc}}
\toprule
& \multicolumn{3}{c}{\textbf{B}} \\
\cmidrule{2-4}
& 2 & 4 & 8 \\
\textbf{A} & \multicolumn{3}{c}{\textbf{value}} \\
\midrule
'a' & & 6 & \\
'b' & 4 & 5 & \\
'c' & 2 & 1 & 3 \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}
65 changes: 65 additions & 0 deletions test/references/listingtable/missing_groups.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<table class="st-cb1156a1">
<style>
.st-cb1156a1 {
border: none;
margin: 0 auto;
padding: 0.25rem;
border-collapse: separate;
border-spacing: 0.85em 0.2em;
line-height: 1.2em;
}

.st-cb1156a1 tr td {
vertical-align: top;
padding: 0;
border: none;
}

.st-cb1156a1 br {
line-height: 0em;
margin: 0;
}

.st-cb1156a1 sub {
line-height: 0;
}

.st-cb1156a1 sup {
line-height: 0;
}
</style>
<tr><td colspan="4" style="border-bottom: 1.5px solid black; padding: 0"></td></tr>
<tr>
<td style="text-align:center;"></td>
<td colspan="3" style="font-weight:bold;border-bottom:1px solid black; padding-bottom: 0.25em;text-align:center;">B</td>
</tr>
<tr>
<td style="text-align:center;"></td>
<td style="text-align:center;">2</td>
<td style="text-align:center;">4</td>
<td style="text-align:center;">8</td>
</tr>
<tr>
<td style="font-weight:bold;text-align:left;">A</td>
<td colspan="3" style="font-weight:bold;text-align:center;">value</td>
</tr>
<tr><td colspan="4" style="border-bottom:1px solid black;padding:0"></td></tr> <tr>
<td style="text-align:left;">&apos;a&apos;</td>
<td style="text-align:center;"></td>
<td style="text-align:center;">6</td>
<td style="text-align:center;"></td>
</tr>
<tr>
<td style="text-align:left;">&apos;b&apos;</td>
<td style="text-align:center;">4</td>
<td style="text-align:center;">5</td>
<td style="text-align:center;"></td>
</tr>
<tr>
<td style="text-align:left;">&apos;c&apos;</td>
<td style="text-align:center;">2</td>
<td style="text-align:center;">1</td>
<td style="text-align:center;">3</td>
</tr>
<tr><td colspan="4" style="border-bottom: 1.5px solid black; padding: 0"></td></tr>
</table>
32 changes: 32 additions & 0 deletions test/references/listingtable/missing_groups.typ.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#table(
rows: 6,
columns: 4,
column-gutter: 0.25em,
align: (left, center, center, center),
stroke: none,
table.hline(y: 0, stroke: 1pt),
[],
table.cell(colspan: 3)[*B*],
table.hline(y: 1, start: 1, end: 4, stroke: 0.75pt),
[],
[2],
[4],
[8],
[*A*],
table.cell(colspan: 3)[*value*],
table.hline(y: 3, stroke: 0.75pt),
['a'],
[],
[6],
[],
['b'],
[4],
[5],
[],
['c'],
[2],
[1],
[3],
table.hline(y: 6, stroke: 1pt),
)
24 changes: 24 additions & 0 deletions test/references/summarytable/missing_groups.latex.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
\documentclass{article}
\usepackage{threeparttable}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{table}[!ht]
\setlength\tabcolsep{0pt}
\centering
\begin{threeparttable}
\begin{tabular}{@{\extracolsep{2ex}}*{5}{llccc}}
\toprule
& & \multicolumn{3}{c}{\textbf{B}} \\
\cmidrule{3-5}
& & 2 & 4 & 8 \\
\textbf{A} & & \multicolumn{3}{c}{\textbf{value}} \\
\midrule
'a' & \textbf{sum} & & 6 & \\
'b' & \textbf{sum} & 4 & 5 & \\
'c' & \textbf{sum} & 2 & 1 & 3 \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}
71 changes: 71 additions & 0 deletions test/references/summarytable/missing_groups.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<table class="st-c751c4b8">
<style>
.st-c751c4b8 {
border: none;
margin: 0 auto;
padding: 0.25rem;
border-collapse: separate;
border-spacing: 0.85em 0.2em;
line-height: 1.2em;
}

.st-c751c4b8 tr td {
vertical-align: top;
padding: 0;
border: none;
}

.st-c751c4b8 br {
line-height: 0em;
margin: 0;
}

.st-c751c4b8 sub {
line-height: 0;
}

.st-c751c4b8 sup {
line-height: 0;
}
</style>
<tr><td colspan="5" style="border-bottom: 1.5px solid black; padding: 0"></td></tr>
<tr>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
<td colspan="3" style="font-weight:bold;border-bottom:1px solid black; padding-bottom: 0.25em;text-align:center;">B</td>
</tr>
<tr>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
<td style="text-align:center;">2</td>
<td style="text-align:center;">4</td>
<td style="text-align:center;">8</td>
</tr>
<tr>
<td style="font-weight:bold;text-align:left;">A</td>
<td style="text-align:center;"></td>
<td colspan="3" style="font-weight:bold;text-align:center;">value</td>
</tr>
<tr><td colspan="5" style="border-bottom:1px solid black;padding:0"></td></tr> <tr>
<td style="text-align:left;">&apos;a&apos;</td>
<td style="font-weight:bold;text-align:left;">sum</td>
<td style="text-align:center;"></td>
<td style="text-align:center;">6</td>
<td style="text-align:center;"></td>
</tr>
<tr>
<td style="text-align:left;">&apos;b&apos;</td>
<td style="font-weight:bold;text-align:left;">sum</td>
<td style="text-align:center;">4</td>
<td style="text-align:center;">5</td>
<td style="text-align:center;"></td>
</tr>
<tr>
<td style="text-align:left;">&apos;c&apos;</td>
<td style="font-weight:bold;text-align:left;">sum</td>
<td style="text-align:center;">2</td>
<td style="text-align:center;">1</td>
<td style="text-align:center;">3</td>
</tr>
<tr><td colspan="5" style="border-bottom: 1.5px solid black; padding: 0"></td></tr>
</table>
38 changes: 38 additions & 0 deletions test/references/summarytable/missing_groups.typ.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

#table(
rows: 6,
columns: 5,
column-gutter: 0.25em,
align: (left, left, center, center, center),
stroke: none,
table.hline(y: 0, stroke: 1pt),
[],
[],
table.cell(colspan: 3)[*B*],
table.hline(y: 1, start: 2, end: 5, stroke: 0.75pt),
[],
[],
[2],
[4],
[8],
[*A*],
[],
table.cell(colspan: 3)[*value*],
table.hline(y: 3, stroke: 0.75pt),
['a'],
[*sum*],
[],
[6],
[],
['b'],
[*sum*],
[4],
[5],
[],
['c'],
[*sum*],
[2],
[1],
[3],
table.hline(y: 6, stroke: 1pt),
)
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ end
sort!(_df, [:group2, :group])
end

df_missing_groups = DataFrame(
value = 1:6,
A = ['c', 'c', 'c', 'b', 'b', 'a'],
B = [4, 2, 8, 2, 4, 4]
)

@testset for func in [as_html, as_latex, as_docx, as_typst]
reftest(t, path) = @testset "$path" run_reftest(t, path, func)

Expand Down Expand Up @@ -345,6 +351,9 @@ end
for (i, page) in enumerate(pt.pages)
reftest(t, "references/listingtable/pagination_rows=2_summarized_grouplevel_1_$i")
end

t = listingtable(df_missing_groups, :value, rows = :A, cols = :B)
reftest(t, "references/listingtable/missing_groups")
end

@testset "summarytable" begin
Expand Down Expand Up @@ -383,6 +392,9 @@ end
@test_throws SortingError t = summarytable(unsortable_df, :value, rows = :parameters, cols = [:group2, :group], summary = [mean])
t = summarytable(unsortable_df, :value, cols = :parameters, rows = [:group2, :group], summary = [mean], sort = false)
reftest(t, "references/summarytable/sort_false")

t = summarytable(df_missing_groups, :value, rows = :A, cols = :B, summary = [sum])
reftest(t, "references/summarytable/missing_groups")
end

@testset "annotations" begin
Expand Down

0 comments on commit b36aa31

Please sign in to comment.