Skip to content

dataframe combining logical masks #14518

Closed

Description

@tino376dev

Describe the bug

I would like to combine logical masks as presented in the documenation

How to reproduce

running:

(true | polars into-df) and (false | polars into-df)

returns:

Error: nu::shell::type_mismatch

  × Type mismatch.
   ╭─[entry #25:1:9]
 1 │ (true | polars into-df) and (false | polars into-df)
   ·         ───────┬──────
   ·                ╰── expected bool
   ╰────

Expected behavior

not error

Configuration

key value
version 0.100.0
major 0
minor 100
patch 0
branch
commit_hash
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.83.0 (90b35a623 2024-11-26)
rust_channel stable-x86_64-unknown-linux-gnu
cargo_version cargo 1.83.0 (5ffbef321 2024-10-29)
build_time 2024-12-03 06:08:48 +01:00
build_rust_channel release
allocator mimalloc
features default, sqlite, trash
installed_plugins polars 0.100.0

Activity

  1. added
    status:needs-triage
    on Dec 5, 2024
  2. added
    A:plugin-polars
    on Dec 5, 2024
  3. fdncred commented on Dec 5, 2024

    @fdncred
    Contributor

    This seems correct to me because and needs a boolean result and true | into-df returns a NuDataFrame which is not a boolean. it's similar to doing 1 and 3, which also produces an error, since 1 and 3 are not bools.

  4. tino376dev commented on Dec 6, 2024

    @tino376dev
    Author

    This seems correct to me because and needs a boolean result and true | into-df returns a NuDataFrame which is not a boolean. it's similar to doing 1 and 3, which also produces an error, since 1 and 3 are not bools.

    How should I interpret the error messages from the following two commands? In test 1 the error suggests the left hand argument is a "NuDataFrame", while the second suggests a "Series of type bool". Am I missing something?

    test 1

    # running
    (true | polars into-df) == true
    
    # returns
    Error: nu::shell::type_mismatch
    
      × Type mismatch during operation.
       ╭─[entry #24:1:9]
     1 │ (true | polars into-df) == true
       ·         ───────┬──────  ─┬ ──┬─
       ·                │         │   ╰── bool
       ·                │         ╰── type mismatch for operator
       ·                ╰── NuDataFrame
       ╰────

    test 2

    # running
    (true | polars into-df) == 0
    
    # returns
    Error:   × Incorrect type
       ╭─[entry #25:1:9]
     1 │ (true | polars into-df) == 0
       ·         ───────┬──────
       ·                ╰── Series of type bool can not be used for operations with an i64 value
       ╰────
  5. fdncred commented on Dec 6, 2024

    @fdncred
    Contributor
    true | polars into-df | describe
    NuDataFrame

    So, test1 is correct. test2 may be reporting the wrong error.

  6. tino376dev commented on Dec 6, 2024

    @tino376dev
    Author

    I still have the impression something is not working correctly. Test 1 and 2 work as expected, but combing masks with the logical operator fails. The documentation (linked in initial comment) suggests it should work.

    # basic setup
    let df = [{a: 1},{a: 2},{a: 3}] | polars into-df
    
    # test 1
    $df | polars set (-1) --mask ($df > 1)
    ╭───┬────╮
    │ # │ a  │
    ├───┼────┤
    │ 0 │  1 │
    │ 1 │ -1 │
    │ 2 │ -1 │
    ╰───┴────╯
    
    # test 2
    $df | polars set (-1) --mask ($df < 3)
    ╭───┬────╮
    │ # │ a  │
    ├───┼────┤
    │ 0 │ -1 │
    │ 1 │ -1 │
    │ 2 │  3 │
    ╰───┴────╯
    
    # test 3
    $df | polars set (-1) --mask (($df > 1) and ($df < 3))
    Error: nu::shell::type_mismatch
    
      × Type mismatch.
       ╭─[entry #11:1:32]
     1 │ $df | polars set (-1) --mask (($df > 1) and ($df < 3))
       ·                                ───┬───
       ·                                   ╰── expected bool
       ╰────
    
    polars set (-1) --mask (($df > 1) and ($df < 3))
  7. fdncred commented on Dec 6, 2024

    @fdncred
    Contributor

    @ayax79 any ideas here?

  8. ayax79 commented on Dec 7, 2024

    @ayax79
    Contributor

    I'll have to dig into this a bit.. I am not as familiar with the workings of polars set.. It does look like the code has a lot of explicit checks for boolean types.

  9. tino376dev commented on Dec 7, 2024

    @tino376dev
    Author

    I'll have to dig into this a bit.. I am not as familiar with the workings of polars set.. It does look like the code has a lot of explicit checks for boolean types.

    I have the impression the error stems from boolean operations of polars series.. the error is throw if you just try and run:

    ($df > 1) and ($df < 3)
  10. ayax79 commented on Dec 9, 2024

    @ayax79
    Contributor

    More investigation. For some reason I don't think the plugin is being called when Operator::Booean(Boolean::And) operations happen.

  11. ayax79 commented on Dec 9, 2024

    @ayax79
    Contributor

    In 0.98 with the new IR feature disabled, the following works.. note that the column needs to be called out directly.

    ($df.a > 1) and ($df.a < 3)

    I'll connect with @devyn some more.. It looks like none of the custom value add stuff is getting called when using IR.

  12. added a commit that references this issue on Dec 21, 2024
    51eaa01
  13. devyn commented on Dec 21, 2024

    @devyn
    Contributor

    I think I've found a fix for this. Thanks for reporting!

    New IR:

    > ([true, false] | polars into-df) and ([true, true] | polars into-df)
    ╭───┬─────────╮
    │ # │ and_0_0 │
    ├───┼─────────┤
    │ 0 │ true    │
    │ 1 │ false   │
    ╰───┴─────────╯
    > view ir { ([true, false] | polars into-df) and ([true, true] | polars into-df) }
    # 3 registers, 18 instructions, 0 bytes of data
       0: load-literal           %0, list(capacity = 2)
       1: load-literal           %1, bool(true)
       2: list-push              %0, %1
       3: load-literal           %1, bool(false)
       4: list-push              %0, %1
       5: redirect-out           value
       6: call                   decl 491 "polars into-df", %0
       7: match                  (false), %0, 16
       8: load-literal           %1, list(capacity = 2)
       9: load-literal           %2, bool(true)
      10: list-push              %1, %2
      11: load-literal           %2, bool(true)
      12: list-push              %1, %2
      13: redirect-out           value
      14: call                   decl 491 "polars into-df", %1
      15: binary-op              %0, Boolean(And), %1
      16: span                   %0          # label(0), from(7:)
      17: return                 %0
    
  14. added this to the v0.102.0 milestone on Dec 25, 2024
  15. added a commit that references this issue on Dec 25, 2024
    35d2750
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:plugin-polarsstatus:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions