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 dseries.struct.explode #9086

Merged
merged 4 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 26 additions & 0 deletions python/dask_cudf/dask_cudf/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ def field(self, key):
meta=self.d_series._meta._constructor([], dtype=typ),
)

def explode(self):
"""
Creates a dataframe view of the struct column, one column per field.

Returns
-------
DataFrame

Examples
--------
>>> import cudf, dask_cudf as dgd
>>> ds = dgd.from_cudf(cudf.Series(
isVoid marked this conversation as resolved.
Show resolved Hide resolved
... [{'a': 42, 'b': 'str1', 'c': [-1]},
... {'a': 0, 'b': 'str2', 'c': [400, 500]},
... {'a': 7, 'b': '', 'c': []}]), npartitions=2)
>>> ds.struct.explode().compute()
a b c
0 42 str1 [-1]
1 0 str2 [400, 500]
2 7 []
"""
return self.d_series.map_partitions(
lambda s: s.struct.explode(),
meta=self.d_series._meta.struct.explode(),
)


class ListMethods:
def __init__(self, d_series):
Expand Down
15 changes: 15 additions & 0 deletions python/dask_cudf/dask_cudf/tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,18 @@ def test_dask_struct_field_Int_Error(data):

with pytest.raises(IndexError):
got.struct.field(1000).compute()


@pytest.mark.parametrize(
"data",
[
[{}, {}, {}],
[{"a": 100, "b": "abc"}, {"a": 42, "b": "def"}, {"a": -87, "b": ""}],
[{"a": [1, 2, 3], "b": {"c": 101}}, {"a": [4, 5], "b": {"c": 102}}],
],
)
def test_struct_explode(data):
expect = pd.DataFrame(data)
isVoid marked this conversation as resolved.
Show resolved Hide resolved
got = dgd.from_cudf(Series(data), 2).struct.explode().compute()

assert_eq(expect, got)