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

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

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 12 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.

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

broadcast_binary_elementwise silently truncates rhs to lhs length
1 participant