Skip to content

fix(polars): add Value::Record to NuExpression::can_downcast logic - #15826

Merged
ayax79 merged 1 commit into
nushell:mainfrom
pyz4:fix-polars-record-downcast
May 27, 2025
Merged

ayax79 merged 1 commit into
nushell:mainfrom
pyz4:fix-polars-record-downcast

Conversation

@pyz4

@pyz4 pyz4 commented May 27, 2025

Copy link
Copy Markdown
Contributor

Description

Merged PR #15553 added the ability to provide expressions in the form of records. This PR conforms the NuExpression::can_downcast logic to account for the newly allowed records argument type. As such, commands that rely on can_downcast in their implementation (e.g., polars with-column) will no longer err when provided with a record. See example below:

#  Current error
> [[a b]; [1 2] [3 4]]
    | polars into-df <-- only works if cast as lazyframe
    | polars with-column {
        c: ((polars col a) * 2)
        d: ((polars col a) * 3)
      }
Error: nu::shell::cant_convert

  × Can't convert to NuDataFrame, NuLazyFrame, NuExpression, NuLazyGroupBy, NuWhen,
  │ NuDataType, NuSchema.
   ╭─[entry #24:3:26]
 2 │         | polars into-df
 3 │ ╭─▶     | polars with-column {
 4 │ │           c: ((polars col a) * 2)
 5 │ │           d: ((polars col a) * 3)
 6 │ ├─▶       }
   · ╰──── can't convert record<c: NuExpression, d: NuExpression> to NuDataFrame, NuLazyFrame, NuExpression, NuLazyGroupBy, NuWhen, NuDataType, NuSchema
   ╰────


# Fixed
> [[a b]; [1 2] [3 4]]
    | polars into-df
    | polars with-column {
        c: ((polars col a) * 2)
        d: ((polars col a) * 3)
      } | polars collect
╭───┬───┬───┬───┬───╮
│ # │ a │ b │ c │ d │
├───┼───┼───┼───┼───┤
│ 0 │ 1 │ 2 │ 2 │ 3 │
│ 1 │ 3 │ 4 │ 6 │ 9 │
╰───┴───┴───┴───┴───╯

User-Facing Changes

No breaking changes

Tests + Formatting

An example test was added to polars with-column.

After Submitting

@github-actions github-actions Bot added A:plugin-polars Work related to the polars dataframe implementation deprecated:pr-plugins Deprecated: use the A:plugins label instead labels May 27, 2025
@ayax79

ayax79 commented May 27, 2025

Copy link
Copy Markdown
Contributor

Thanks!

@ayax79
ayax79 merged commit ae51f6d into nushell:main May 27, 2025
@github-actions github-actions Bot added this to the v0.105.0 milestone May 27, 2025
@pyz4
pyz4 deleted the fix-polars-record-downcast branch June 1, 2025 12:12
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 deprecated:pr-plugins Deprecated: use the A:plugins label instead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants