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

Add DataFrame.to_struct() method to convert a DataFrame to a struct Series #8728

Merged
merged 6 commits into from
Jul 15, 2021
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
18 changes: 11 additions & 7 deletions python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -2037,13 +2037,20 @@ def as_column(
np_type = None
try:
if dtype is not None:
if is_categorical_dtype(dtype) or is_interval_dtype(dtype):
raise TypeError
if is_list_dtype(dtype):
data = pa.array(arbitrary)
if type(data) not in (pa.ListArray, pa.NullArray):
raise ValueError(
"Cannot create list column from given data"
)
return as_column(data, nan_as_null=nan_as_null)
elif isinstance(
dtype, cudf.StructDtype
) and not isinstance(dtype, cudf.IntervalDtype):
data = pa.array(arbitrary, type=dtype.to_arrow())
return as_column(data, nan_as_null=nan_as_null)
if isinstance(dtype, cudf.core.dtypes.Decimal64Dtype):
data = pa.array(
arbitrary,
Expand All @@ -2065,14 +2072,11 @@ def as_column(
data
)
dtype = pd.api.types.pandas_dtype(dtype)
if is_categorical_dtype(dtype) or is_interval_dtype(dtype):
raise TypeError
np_type = np.dtype(dtype).type
if np_type == np.bool_:
pa_type = pa.bool_()
else:
np_type = np.dtype(dtype).type
if np_type == np.bool_:
pa_type = pa.bool_()
else:
pa_type = np_to_pa_dtype(np.dtype(dtype))
pa_type = np_to_pa_dtype(np.dtype(dtype))
data = as_column(
pa.array(
arbitrary,
Expand Down
19 changes: 19 additions & 0 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7477,6 +7477,25 @@ def to_dict(self, orient="dict", into=dict):
"`.to_pandas().to_dict()` to construct a Python dictionary."
)

def to_struct(self, name=None):
"""
Return a struct Series composed of the columns of the DataFrame.
Note that no copies of the data are made.

Parameters
----------
name: optional
Name of the resulting Series
"""
col = cudf.core.column.build_struct_column(
names=self._data.names, children=self._data.columns, size=len(self)
)
return cudf.Series._from_data(
cudf.core.column_accessor.ColumnAccessor({name: col}),
index=self.index,
name=name,
)

def keys(self):
"""
Get the columns.
Expand Down
14 changes: 14 additions & 0 deletions python/cudf/cudf/tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,17 @@ def test_struct_scalar_host_construction(data):
def test_struct_scalar_null():
slr = cudf.Scalar(cudf.NA, dtype=StructDtype)
assert slr.device_value.value is cudf.NA


def test_dataframe_to_struct():
df = cudf.DataFrame()
expect = cudf.Series(dtype=cudf.StructDtype({}))
got = df.to_struct()
assert_eq(expect, got)

df = cudf.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"]})
expect = cudf.Series(
[{"a": 1, "b": "x"}, {"a": 2, "b": "y"}, {"a": 3, "b": "z"}]
)
got = df.to_struct()
assert_eq(expect, got)