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 __setitem__ on string columns when the scalar value ends in a null byte #12991

Merged
merged 3 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions python/cudf/cudf/tests/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,18 @@ def test_scatter_by_slice_with_start_and_step():
target[1::2] = source
ctarget[1::2] = csource
assert_eq(target, ctarget)


@pytest.mark.parametrize("n", [1, 3])
def test_setitem_str_trailing_null(n):
trailing_nulls = "\x00" * n
s = cudf.Series(["a", "b", "c" + trailing_nulls])
assert s[2] == "c" + trailing_nulls
s[0] = "a" + trailing_nulls
assert s[0] == "a" + trailing_nulls
s[1] = trailing_nulls
assert s[1] == trailing_nulls
s[0] = ""
assert s[0] == ""
s[0] = "\x00"
assert s[0] == "\x00"
9 changes: 9 additions & 0 deletions python/cudf/cudf/utils/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ def to_cudf_compatible_scalar(val, dtype=None):
) or cudf.api.types.is_string_dtype(dtype):
dtype = "str"

if isinstance(val, str) and val.endswith("\x00"):
# Numpy string dtypes are fixed width and use NULL to
# indicate the end of the string, so they cannot
# distinguish between "abc\x00" and "abc".
# https://github.com/numpy/numpy/issues/20118
# In this case, don't try going through numpy and just use
# the string value directly (cudf.DeviceScalar will DTRT)
return val

if isinstance(val, datetime.datetime):
val = np.datetime64(val)
elif isinstance(val, datetime.timedelta):
Expand Down