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

Convert Column Name to String Before Using Struct Column Factory #10156

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -1654,8 +1654,8 @@ def build_struct_column(

Parameters
----------
names : list-like
Field names to map to children dtypes
names : sequence of strings
Field names to map to children dtypes, must be strings.
children : tuple

mask: Buffer
Expand Down
12 changes: 11 additions & 1 deletion python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5866,8 +5866,18 @@ def to_struct(self, name=None):
-----
Note that a copy of the columns is made.
"""
if not all(isinstance(name, str) for name in self._data.names):
warnings.warn(
"DataFrame contains non-string column name(s). Struct column "
"require string as field name. Non-string column names will "
isVoid marked this conversation as resolved.
Show resolved Hide resolved
"be casted to string as the field name."
)
field_names = [str(name) for name in self._data.names]
else:
field_names = self._data.names
isVoid marked this conversation as resolved.
Show resolved Hide resolved

col = cudf.core.column.build_struct_column(
names=self._data.names, children=self._data.columns, size=len(self)
names=field_names, children=self._data.columns, size=len(self)
)
return cudf.Series._from_data(
cudf.core.column_accessor.ColumnAccessor(
Expand Down
10 changes: 10 additions & 0 deletions python/cudf/cudf/tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ def test_dataframe_to_struct():
df["a"][0] = 5
assert_eq(got, expect)

# check that a non-string (but convertible to string) named column can be
# converted to struct
with pytest.warns(UserWarning, match="will be casted"):
isVoid marked this conversation as resolved.
Show resolved Hide resolved
df = cudf.DataFrame([[1, 2], [3, 4]], columns=[(1, "b"), 0])
expect = cudf.Series(
[{"(1, 'b')": 1, "0": 2}, {"(1, 'b')": 3, "0": 4}]
)
got = df.to_struct()
assert_eq(got, expect)


@pytest.mark.parametrize(
"series, slce",
Expand Down