Skip to content

feat(polars): pivot: provide default --on-cols value if not specified - #18140

Merged
ayax79 merged 1 commit into
nushell:mainfrom
pyz4:feat-polars-pivot-oncols
Apr 29, 2026
Merged

ayax79 merged 1 commit into
nushell:mainfrom
pyz4:feat-polars-pivot-oncols

Conversation

@pyz4

@pyz4 pyz4 commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Description

This PR removes the requirement in polars pivot that --on-cols must be specified. Mirroring the behavior in Python polars.pivot (https://github.com/pola-rs/polars/blob/py-1.40.1/py-polars/src/polars/dataframe/frame.py#L9456-L9683), polars pivot now supplies default values for --on-cols by selecting unique values from the columns specified by --on.

See examples below

# Given a set of test scores, reshape so we have one row per student, with different subjects as columns, and their test_1 scores as values
  > {
        "name": ["Cady", "Cady", "Karen", "Karen"],
        "subject": ["maths", "physics", "maths", "physics"],
        "test_1": [98, 99, 61, 58],
        "test_2": [100, 100, 60, 60],
    } |
    polars into-df --as-columns |
    polars pivot --on subject --on-cols [maths physics] --index name --values test_1 |
    polars sort-by name maths physics |
    polars collect
  ╭───┬───────┬───────┬─────────╮
  │ # │ name  │ maths │ physics │
  ├───┼───────┼───────┼─────────┤
  │ 0 │ Cady  │    98 │      99 │
  │ 1 │ Karen │    61 │      58 │
  ╰───┴───────┴───────┴─────────╯

 # Given a set of test scores, reshape so we have one row per student, with different subjects as columns, and their test_1 scores as values, without specifying --on-cols
  > {
        "name": ["Cady", "Cady", "Karen", "Karen"],
        "subject": ["maths", "physics", "maths", "physics"],
        "test_1": [98, 99, 61, 58],
        "test_2": [100, 100, 60, 60],
    } |
    polars into-df --as-columns |
    polars pivot --on subject --index name --values test_1 |
    polars sort-by name maths physics |
    polars collect
  ╭───┬───────┬───────┬─────────╮
  │ # │ name  │ maths │ physics │
  ├───┼───────┼───────┼─────────┤
  │ 0 │ Cady  │    98 │      99 │
  │ 1 │ Karen │    61 │      58 │
  ╰───┴───────┴───────┴─────────╯

User-facing changes (Release notes)

Refactor --on-cols in polars pivot from required to optional parameter; supply default values based on unique values of --on columns

Additional notes

@github-actions github-actions Bot added A:plugin-polars Work related to the polars dataframe implementation A:plugins This issue is about plugins labels Apr 29, 2026
@fdncred

fdncred commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

@pyz4 Please remove the changes to agents.md/claude.md and fix the clippy lints and we can proceed.

@pyz4
pyz4 force-pushed the feat-polars-pivot-oncols branch from 1d53aba to 9626a36 Compare April 29, 2026 15:16
@pyz4
pyz4 force-pushed the feat-polars-pivot-oncols branch from 9626a36 to ef36770 Compare April 29, 2026 15:25
@pyz4

pyz4 commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

@fdncred Done

@ayax79

ayax79 commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Looks good to me.

@ayax79
ayax79 merged commit f972c80 into nushell:main Apr 29, 2026
17 checks passed
@github-actions github-actions Bot added this to the v0.113.0 milestone Apr 29, 2026
@pyz4
pyz4 deleted the feat-polars-pivot-oncols branch April 29, 2026 21:31
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:other Noted in "Other changes" section labels May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:plugin-polars Work related to the polars dataframe implementation A:plugins This issue is about plugins notes:other Noted in "Other changes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants