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

fix: Ensure mean_horizontal raises on non-numeric input #19648

Merged
merged 2 commits into from
Nov 6, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Nov 5, 2024

The mean_horizontal expression was the only specialised horizontal function that wouldn't raise an error on incompatible input - instead it just filtered that input for the numeric/boolean columns, checked there was at least one, and operated on those.

This is inconsistent; selectors can be used to trivially choose numeric columns - the expression shouldn't be doing it silently (and it isn't documented that it would do so).

Incompatible input now raises a clear error.

Also:

Allow Decimal in mean_horizontal (was omitted by an .is_numeric() dtype check).

Example

  • Catch invalid input
    from decimal import Decimal as D
    import polars as pl
    
    df = pl.DataFrame({
        "cola": [D("1.5"), D("0.5"), D("5"), D("0"), D("-0.25")],
        "colb": [[0,1], [2], [3,4], [5], [6]],
        "colc": ["aa", "bb", "cc", "dd", "ee"],
        "cold": ["bb", "cc", "dd", "ee", "ff"],
        "cole": [1000, 2000, 3000, 4000, 5000],
    })
    df.select(
        pl.mean_horizontal(
            pl.col("cola"),
            pl.col("colb"),
            pl.col("colc"),
            pl.col("cold"),
            pl.col("cole"),
        )
    )
    Before:
    # shape: (5, 1)
    # ┌────────┐
    # │ cola   │
    # │ ---    │
    # │ f64    │
    # ╞════════╡
    # │ 1000.0 │
    # │ 2000.0 │
    # │ 3000.0 │
    # │ 4000.0 │
    # │ 5000.0 │
    # └────────┘
    After:
    # InvalidOperationError: 
    # 'horizontal_mean' expects numeric expressions, found "colb" (dtype=list[i64])

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Nov 5, 2024
Copy link

codecov bot commented Nov 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.92%. Comparing base (047e578) to head (88e250d).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #19648   +/-   ##
=======================================
  Coverage   79.92%   79.92%           
=======================================
  Files        1536     1536           
  Lines      211700   211703    +3     
  Branches     2445     2445           
=======================================
+ Hits       169199   169202    +3     
  Misses      41946    41946           
  Partials      555      555           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good fix. Just a mental note to us, that we ideally check this in the IR resolve phase.

@ritchie46 ritchie46 merged commit 10abada into pola-rs:main Nov 6, 2024
28 checks passed
@alexander-beedie alexander-beedie deleted the fix-mean-horizontal branch November 6, 2024 10:38
@alexander-beedie
Copy link
Collaborator Author

Good fix. Just a mental note to us, that we ideally check this in the IR resolve phase.

We should likely allow temporal expressions here too (then validate the supertype of all inputs), but one thing at a time :)

tylerriccio33 pushed a commit to tylerriccio33/polars that referenced this pull request Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants