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: Error instead of truncate if length mismatch for several str functions #20781

Merged
merged 6 commits into from
Jan 20, 2025

Conversation

etiennebacher
Copy link
Contributor

@etiennebacher etiennebacher commented Jan 18, 2025

Fixes #16070

The original issue only mentioned str.zfill(). There was already a similar check for some functions (e.g. str.head()). This PR extends this check to more str functions and adds tests, but I did not check for other namespaces.

Before (1.20.0): silently truncate to parameter length

import polars as pl

input = pl.Series(["foo", "bar", "baz"])

input.str.starts_with(pl.Series(["a", "b"]))
input.str.ends_with(pl.Series(["a", "b"]))
input.str.contains(pl.Series(["a", "b"]))
input.str.find(pl.Series(["a", "b"]))
input.str.zfill(pl.Series([1, 2]))
input.str.strip_chars(pl.Series(["a", "b"]))
input.str.strip_chars_start(pl.Series(["a", "b"]))
input.str.strip_chars_end(pl.Series(["a", "b"]))
input.str.strip_prefix(pl.Series(["a", "b"]))
input.str.strip_suffix(pl.Series(["a", "b"]))
shape: (2,)
Series: '' [bool]
[
        false
        true
]
shape: (2,)
Series: '' [bool]
[
        false
        false
]
shape: (2,)
Series: '' [bool]
[
        false
        true
]
shape: (2,)
Series: '' [u32]
[
        null
        0
]
shape: (2,)
Series: '' [str]
[
        "foo"
        "bar"
]
shape: (2,)
Series: '' [str]
[
        "foo"
        "ar"
]
shape: (2,)
Series: '' [str]
[
        "foo"
        "ar"
]
shape: (2,)
Series: '' [str]
[
        "foo"
        "bar"
]
shape: (2,)
Series: '' [str]
[
        "foo"
        "ar"
]
shape: (2,)
Series: '' [str]
[
        "foo"
        "bar"
]

Now: error

polars.exceptions.ComputeError: all series in `str.starts_with()` should have equal or unit length

... etc() ...

Copy link

codecov bot commented Jan 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.78%. Comparing base (28a45f2) to head (8fbc4ab).
Report is 25 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #20781      +/-   ##
==========================================
+ Coverage   79.73%   79.78%   +0.04%     
==========================================
  Files        1561     1561              
  Lines      221910   221994      +84     
  Branches     2530     2531       +1     
==========================================
+ Hits       176938   177115     +177     
+ Misses      44390    44297      -93     
  Partials      582      582              

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

@ritchie46 ritchie46 changed the title fix(python): Error instead of truncate if length mismatch for several str functions fix: Error instead of truncate if length mismatch for several str functions Jan 20, 2025
@github-actions github-actions bot added the rust Related to Rust Polars label Jan 20, 2025
@ritchie46 ritchie46 merged commit 099ee3c into pola-rs:main Jan 20, 2025
29 checks passed
@etiennebacher etiennebacher deleted the str-unequal-length branch January 20, 2025 16:39
@@ -466,6 +466,7 @@ fn extract_many(
ascii_case_insensitive: bool,
overlapping: bool,
) -> PolarsResult<Column> {
_check_same_length(s, "extract_many")?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this change really necessary?
This change broke the R API by preventing the use of str type Series of longer than length 2 as input.

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.

broadcast_binary_elementwise silently truncates rhs to lhs length
3 participants