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

String-to-boolean conversion is different from Pandas #8549

Merged
merged 11 commits into from
Jul 1, 2021
Merged
8 changes: 7 additions & 1 deletion python/cudf/cudf/core/column/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@
is_string_dtype,
)


def str_to_boolean(column: StringColumn):
"""Takes in string column and returns boolean column """
return (column.str().len() > cudf.Scalar(0, dtype="int8")).fillna(False)


_str_to_numeric_typecast_functions = {
np.dtype("int8"): str_cast.stoi8,
np.dtype("int16"): str_cast.stoi16,
Expand All @@ -182,7 +188,7 @@
np.dtype("uint64"): str_cast.stoul,
np.dtype("float32"): str_cast.stof,
np.dtype("float64"): str_cast.stod,
np.dtype("bool"): str_cast.to_booleans,
np.dtype("bool"): str_to_boolean,
}

_numeric_to_str_typecast_functions = {
Expand Down
6 changes: 1 addition & 5 deletions python/cudf/cudf/tests/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ def test_string_astype(dtype):
ps = pd.Series(data)
gs = cudf.Series(data)

# Pandas str --> bool typecasting always returns True if there's a string
if dtype.startswith("bool"):
expect = ps == "True"
else:
expect = ps.astype(dtype)
expect = ps.astype(dtype)
got = gs.astype(dtype)

assert_eq(expect, got)
Expand Down