Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multilevel tables #175

Open
jariji opened this issue Sep 3, 2022 · 6 comments
Open

Multilevel tables #175

jariji opened this issue Sep 3, 2022 · 6 comments

Comments

@jariji
Copy link
Contributor

jariji commented Sep 3, 2022

julia> using PrettyTables, DataFrames

julia> pretty_table(DataFrame(x=[1,2], t=[(;a=10,b=20), (;a=100,b=200)]))
┌───────┬───────────────────────────────────────────┐
│     x │                                         t │
│ Int64 │ NamedTuple{(:a, :b), Tuple{Int64, Int64}} │
├───────┼───────────────────────────────────────────┤
│     1 │                          (a = 10, b = 20) │
│     2 │                        (a = 100, b = 200) │
└───────┴───────────────────────────────────────────┘

It would be useful if a hierarchical table could be rendered hierarchically, like in Pandas. Especially for Latex output when I have hierarchical data. Currently I can use headers= manually, but it would be nice if it could happen automatically.

first        bar                 baz                 foo                 qux          
second       one       two       one       two       one       two       one       two
A       0.895717  0.805244 -1.206412  2.565646  1.431256  1.340309 -1.170299 -0.226169
B       0.410835  0.813850  0.132003 -0.827317 -0.076467 -1.187678  1.130127 -1.436737
C      -1.413681  1.607920  1.024180  0.569605  0.875906 -2.211372  0.974466 -2.006747
@ronisbr
Copy link
Owner

ronisbr commented Sep 4, 2022

Hi @jariji !

A feature like this requires that the printing mechanism is aware of the data it is printing. In PrettyTables.jl, we treat everything as a 2D table. This additional process would be performed entirely before printing the table. To keep things simple inside PrettyTables.jl, I think it is better to add this functionality in another package that depends on PrettyTables.jl. This new package would pre-process the data and pass it to PrettyTables to render it. Makes sense?

@bkamins
Copy link

bkamins commented Sep 5, 2022

This essentially, at low level, requires "spanner column label" support (x-ref https://gt.rstudio.com/index.html). The other question is how to signal printing mechanizm that spanner should be used, but first even a possibility of such spanner would have to be added.

@jariji - to start with could you maybe show how you want DataFrame(x=[1,2], t=[(;a=10,b=20), (;a=100,b=200)], y=[1,2]) (I have added y column for better clarity) to be displayed exactly in text/plain?

@jariji
Copy link
Contributor Author

jariji commented Sep 5, 2022

I had in mind a terminal display like this

x   t         y    
    a    b    
1  10  100    1  
2  20  200    2

I'm currently making LaTeX tables and I would like to have the "spanner" output t centered above a b.

@bkamins
Copy link

bkamins commented Sep 5, 2022

Thank you, but we need to be more precise than this (sorry for being picky, but such design discussions are usually detailed - probably you saw it already in DataFrames.jl issues).

The current way this table is printed in text/plain is:

2×3 DataFrame
 Row │ x      t                   y
     │ Int64  NamedTup…           Int64
─────┼──────────────────────────────────
   1 │     1  (a = 10, b = 20)        1
   2 │     2  (a = 100, b = 200)      2

do you want it to be instead:

2×3 DataFrame
 Row │ x      t             y
     │        a      b       
     │ Int64  Int64  Int64  Int64
─────┼────────────────────────────
   1 │     1     10     20      1
   2 │     2    100    200      2

While we are at it - is one level of such unnesting enough or would you expect it to support more levels of unnesting?

(I am not saying that this is what @ronisbr can do - I have not asked him about it, but to start with I think we need precise requirements and then, 1) @ronisbr can decide what is possible, 2) we need to discuss the API - the point is, as @ronisbr noted, that it would have to be consistent across Tables.jl tables)

I know that you are working with LaTeX backend now. In PrettyTables.jl LaTeX has not been worked on yet.

However, in general, if we add some functionality I assume that @ronisbr prefers it to be consistent across plain text, HTML, and LaTeX backends (as otherwise very quickly the complexity of the options will not be manageable).

@ronisbr
Copy link
Owner

ronisbr commented Sep 6, 2022

By the way, this table can be printed as it is if we do not need to align the label t in the center of the two cells below (a and b). The only problem is that PrettyTables should be aware that a named tuple in the column t actually means two columns.

We can go two ways here:

  1. Add a switch to always split a NamedTuple into the corresponding columns. However, there are some difficult edge cases and I think it is too much to add inside PrettyTables. It will make the maintainability a lot harder.
  2. Develop some kind of printing recipe for custom types. We need to figure out some kind of API that will allow the user to tell pretty tables how to render each cell. If it must be split into multiple columns, into multiple rows, if a cell should be merged with another one, etc. This approach is the best one, but creating a good mechanism can be very difficult.

@bkamins
Copy link

bkamins commented Sep 6, 2022

This approach is the best one, but creating a good mechanism can be very difficult.

I agree it would be best. In particular we should dispatch such a recipe on column eltype (not values as this would be even harder I think)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants