Skip to content

fix: Raise if n or fraction cannot be cast in sample() for DataFrame and Expr #22310

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

etiennebacher
Copy link
Contributor

Fixes #21521

Before

import polars as pl

df = pl.DataFrame({"a": [1], "foo": ["x"]})

### df
df.sample(fraction="foo")
# shape: (0, 2)
# ┌─────┬─────┐
# │ a   ┆ foo │
# │ --- ┆ --- │
# │ i64 ┆ str │
# ╞═════╪═════╡
# └─────┴─────┘
df.sample(n="foo")
shape: (0, 2)
# ┌─────┬─────┐
# │ a   ┆ foo │
# │ --- ┆ --- │
# │ i64 ┆ str │
# ╞═════╪═════╡
# └─────┴─────┘

### expr
df.select(pl.col("a").sample(fraction="foo"))
# shape: (0, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# └─────┘

df.select(pl.col("a").sample(n="foo"))
# shape: (0, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# └─────┘

After

import polars as pl

df = pl.DataFrame({"a": [1], "foo": ["x"]})

### df
df.sample(fraction="foo")
# polars.exceptions.InvalidOperationError: conversion from `str` to `f64` failed in column 'frac' for 1 out of 1 values: ["foo"]
df.sample(n="foo")
# polars.exceptions.InvalidOperationError: conversion from `str` to `u32` failed in column '' for 1 out of 1 values: ["foo"]

### expr
df.select(pl.col("a").sample(fraction="foo"))
# polars.exceptions.InvalidOperationError: conversion from `str` to `f64` failed in column 'foo' for 1 out of 1 values: ["x"]
df.select(pl.col("a").sample(n="foo"))
# polars.exceptions.InvalidOperationError: conversion from `str` to `u32` failed in column 'foo' for 1 out of 1 values: ["x"]

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Apr 16, 2025
Copy link

codecov bot commented Apr 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.63%. Comparing base (5cec049) to head (44c249a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #22310   +/-   ##
=======================================
  Coverage   80.63%   80.63%           
=======================================
  Files        1677     1677           
  Lines      222209   222209           
  Branches     2801     2801           
=======================================
+ Hits       179178   179185    +7     
+ Misses      42364    42356    -8     
- Partials      667      668    +1     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@etiennebacher etiennebacher marked this pull request as draft May 26, 2025 11:57
@etiennebacher etiennebacher marked this pull request as ready for review May 26, 2025 19:05
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.

In sample() for DataFrame, fraction and n should error if they are not numeric
1 participant