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

Apply type metadata after column is slice-copied #9131

Merged
merged 3 commits into from
Aug 27, 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
5 changes: 4 additions & 1 deletion python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@ def slice(self, start: int, stop: int, stride: int = None) -> ColumnBase:
return column_empty(0, self.dtype, masked=True)
# compute mask slice
if stride == 1:
return libcudf.copying.column_slice(self, [start, stop])[0]
return libcudf.copying.column_slice(self, [start, stop])[
0
]._with_type_metadata(self.dtype)
else:
# Need to create a gather map for given slice with stride
gather_map = arange(
Expand Down Expand Up @@ -721,6 +723,7 @@ def take(
self.as_frame()
._gather(indices, keep_index=keep_index, nullify=nullify)
._as_column()
._with_type_metadata(self.dtype)
)
except RuntimeError as e:
if "out of bounds" in str(e):
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/column/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __getitem__(self, args):
field: value
for field, value in zip(self.dtype.fields, result.values())
}
return result._rename_fields(self.dtype.fields.keys())
return result

def __setitem__(self, key, value):
if isinstance(value, dict):
Expand Down
12 changes: 12 additions & 0 deletions python/cudf/cudf/tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ def test_struct_slice(series, start, end):
assert sr[start:end].to_arrow() == expected.to_arrow()


def test_struct_slice_nested_struct():
data = [
{"a": {"b": 42, "c": "abc"}},
{"a": {"b": 42, "c": "hello world"}},
]

got = cudf.Series(data)[0:1]
expect = cudf.Series(data[0:1])
assert got.__repr__() == expect.__repr__()
assert got.dtype.to_arrow() == expect.dtype.to_arrow()


@pytest.mark.parametrize(
"data",
[
Expand Down