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

[REVIEW] Fix StructColumn.to_pandas type handling issues #9388

Merged
merged 5 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 11 additions & 0 deletions python/cudf/cudf/core/column/struct.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) 2020, NVIDIA CORPORATION.
from __future__ import annotations

import pandas as pd
import pyarrow as pa

import cudf
Expand Down Expand Up @@ -80,6 +81,16 @@ def to_arrow(self):
pa_type, len(self), buffers, children=children
)

def to_pandas(self, index: pd.Index = None, **kwargs) -> "pd.Series":
# We cannot go via Arrow's `to_pandas` because of the following issue:
# https://issues.apache.org/jira/browse/ARROW-12680

pd_series = pd.Series(self.to_arrow().tolist(), dtype="object")

if index is not None:
pd_series.index = index
return pd_series

def __getitem__(self, args):
result = super().__getitem__(args)
if isinstance(result, dict):
Expand Down
28 changes: 27 additions & 1 deletion python/cudf/cudf/tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import cudf
from cudf.core.dtypes import StructDtype
from cudf.testing._utils import assert_eq
from cudf.testing._utils import DATETIME_TYPES, TIMEDELTA_TYPES, assert_eq


@pytest.mark.parametrize(
Expand Down Expand Up @@ -292,3 +292,29 @@ def test_struct_field_errors(data):

with pytest.raises(IndexError):
got.struct.field(100)


@pytest.mark.parametrize("dtype", DATETIME_TYPES + TIMEDELTA_TYPES)
def test_struct_with_datetime_and_timedelta(dtype):
df = cudf.DataFrame(
{
"a": [12, 232, 2334],
"datetime": cudf.Series([23432, 3432423, 324324], dtype=dtype),
}
)
series = df.to_struct()

actual = series.to_pandas()
expected = pd.Series(series.to_arrow().tolist())
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
assert_eq(expected, actual)


def test_struct_int_values():
series = cudf.Series(
[{"a": 1, "b": 2}, {"a": 10, "b": None}, {"a": 5, "b": 6}]
)
actual_series = series.to_pandas()

assert isinstance(actual_series[0]["b"], int)
assert isinstance(actual_series[1]["b"], type(None))
assert isinstance(actual_series[2]["b"], int)