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

group-by should be able to output arrays too #12772

Open
13x1 opened this issue May 5, 2024 · 2 comments
Open

group-by should be able to output arrays too #12772

13x1 opened this issue May 5, 2024 · 2 comments
Labels
enhancement New feature or request needs-triage An issue that hasn't had any proper look

Comments

@13x1
Copy link

13x1 commented May 5, 2024

Related problem

Short example:

> [0 0 1 0 2 3 0 3 0 2 1 2] | group-by
╭───┬───────────╮
│   │ ╭───┬───╮ │
│ 0 │ │ 0 │ 0 │ │
│   │ │ 1 │ 0 │ │
│   │ │ 2 │ 0 │ │
│   │ │ 3 │ 0 │ │
│   │ │ 4 │ 0 │ │
│   │ ╰───┴───╯ │
│   │ ╭───┬───╮ │
│ 1 │ │ 0 │ 1 │ │
│   │ │ 1 │ 1 │ │
│   │ ╰───┴───╯ │
│   │ ╭───┬───╮ │
│ 2 │ │ 0 │ 2 │ │
│   │ │ 1 │ 2 │ │
│   │ │ 2 │ 2 │ │
│   │ ╰───┴───╯ │
│   │ ╭───┬───╮ │
│ 3 │ │ 0 │ 3 │ │
│   │ │ 1 │ 3 │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯
> [[0,0,0,0,0],[1,1],[2,2,2],[3,3]]
╭───┬───────────╮
│ 0 │ ╭───┬───╮ │
│   │ │ 0 │ 0 │ │
│   │ │ 1 │ 0 │ │
│   │ │ 2 │ 0 │ │
│   │ │ 3 │ 0 │ │
│   │ │ 4 │ 0 │ │
│   │ ╰───┴───╯ │
│ 1 │ ╭───┬───╮ │
│   │ │ 0 │ 1 │ │
│   │ │ 1 │ 1 │ │
│   │ ╰───┴───╯ │
│ 2 │ ╭───┬───╮ │
│   │ │ 0 │ 2 │ │
│   │ │ 1 │ 2 │ │
│   │ │ 2 │ 2 │ │
│   │ ╰───┴───╯ │
│ 3 │ ╭───┬───╮ │
│   │ │ 0 │ 3 │ │
│   │ │ 1 │ 3 │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯

You would expect those to work the same, but they don't:

> [0 0 1 0 2 3 0 3 0 2 1 2] | group-by | get 0
Error: nu::shell::type_mismatch

  × Type mismatch.
   ╭─[entry #156:1:1]
 1 │ [0 0 1 0 2 3 0 3 0 2 1 2] | group-by | get 0
   ·                                            ┬
   ·                                            ╰── Can't access record values with a row index. Try specifying a column name instead
   ╰────

That's because one of them is a record with keys "0", "1", "2", and "3", and one is actually an array.

Describe the solution you'd like

Allow group-by to output arrays (probably behind a flag in order to not break things), and maybe an into array command or an enumerare -r (similar to transpose -r) or something like that to fix problems like this.

Describe alternatives you've considered

You can work around it with a few into ints, but it's annoying.

Additional context and details

No response

@13x1 13x1 added enhancement New feature or request needs-triage An issue that hasn't had any proper look labels May 5, 2024
@sholderbach
Copy link
Member

sholderbach commented May 5, 2024

This seems related to #12226 and the discussion there making group-by --to-table the default behavior.
In the default behavior the groups are records with "columns" which have strictly strings as keys.

@fdncred
Copy link
Collaborator

fdncred commented May 5, 2024

I think the *-by command work with a column name and since lists don't have column names maybe it doesn't work. You could always wrap it like this.

 [0 0 1 0 2 3 0 3 0 2 1 2] | wrap name | group-by name | table -e                                                      1  02:02:24 PM
╭───┬──────────────╮
    ╭─#─┬─name─╮ │
 0   0     0  
     1     0  
     2     0  
     3     0  
     4     0  
    ╰─#─┴─name─╯ │
    ╭─#─┬name╮   │
 1   0   1    
     1   1    
    ╰───┴────╯   
    ╭─#─┬name╮   │
 2   0   2    
     1   2    
     2   2    
    ╰───┴────╯   
    ╭─#─┬name╮   │
 3   0   3    
     1   3    
    ╰───┴────╯   
╰───┴──────────────╯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs-triage An issue that hasn't had any proper look
Projects
None yet
Development

No branches or pull requests

3 participants