Repository navigation
fix: str.slice(..., length=0) returns empty string on pandas and dask - #3891
Merged
Merged
Conversation
The pandas-like and dask backends computed the slice stop as `offset + length if length else None`, so a valid length of 0 is falsy and treated as None, meaning 'slice to the end' instead of returning an empty string. str.slice(1, 0) returned the rest of the string on pandas/dask while Polars, PyArrow and the SQL backends correctly returned ''. Use `length is not None` to match them.
str.slice(..., length=0) returns empty string on pandas and dask
FBruzzesi
approved these changes
Aug 31, 2026
FBruzzesi
left a comment
Member
There was a problem hiding this comment.
Thanks for spotting the issue and fixing it @winklemad 🙏🏼
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
I noticed
str.slice(offset, length=0)gives different results depending on the backend. On pandas and dask it returns the rest of the string, but on Polars, PyArrow and the SQL backends it correctly returns an empty string:The cause is in
_pandas_like/series_str.pyand_dask/expr_str.py, which compute the stop asoffset + length if length else None. Alengthof0is falsy, so it's treated the same asNone("slice to the end"). The Arrow and SQL backends already uselength is not None, so this just brings pandas and dask in line with them.What type of PR is this? (check all applicable)
Related issues
Found while reading the string namespace, no existing issue.
AI assistance
I used Claude Code to help find the divergence and draft the test. I read the code, reproduced it across all backends, and understand and stand behind every line.
Checklist
pytestpassing locally (not CI):This is my first PR here. I can't attach an image in the PR body, so here is the terminal output of the slice tests passing across the backends I have installed locally (pandas, pandas[pyarrow], pyarrow, polars eager+lazy, dask):
The new
(1, 0, {"a": ["", ""]})case fails onmainfor pandas/dask and passes with this change; happy to attach a screenshot too if you'd prefer.