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

Fix ListColumn.to_pandas() to retain list type #15155

Merged
merged 5 commits into from
Mar 4, 2024
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: 18 additions & 0 deletions python/cudf/cudf/core/column/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import List, Optional, Sequence, Tuple, Union

import numpy as np
import pandas as pd
import pyarrow as pa
from typing_extensions import Self

Expand Down Expand Up @@ -288,6 +289,23 @@ def _transform_leaves(self, func, *args, **kwargs) -> Self:
)
return lc

def to_pandas(
self,
*,
index: Optional[pd.Index] = None,
nullable: bool = False,
) -> pd.Series:
# Can't rely on Column.to_pandas implementation for lists.
# Need to perform `to_pylist` to preserve list types.
Comment on lines +298 to +299
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably this is some bug in how list dtypes are handled in arrow? Do we need to report something upstream?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already an open issue: apache/arrow#34574

if nullable:
raise NotImplementedError(f"{nullable=} is not implemented.")

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

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


class ListMethods(ColumnMethods):
"""
Expand Down
4 changes: 3 additions & 1 deletion python/cudf/cudf/tests/test_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.

import functools
import operator
Expand Down Expand Up @@ -41,6 +41,8 @@ def test_create_list_series(data):
expect = pd.Series(data)
got = cudf.Series(data)
assert_eq(expect, got)
assert isinstance(got[0], type(expect[0]))
assert isinstance(got.to_pandas()[0], type(expect[0]))


@pytest.mark.parametrize(
Expand Down
6 changes: 1 addition & 5 deletions python/dask_cudf/dask_cudf/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,13 +702,9 @@ def test_is_supported(arg, supported):

def test_groupby_unique_lists():
df = pd.DataFrame({"a": [0, 0, 0, 1, 1, 1], "b": [10, 10, 10, 7, 8, 9]})
ddf = dd.from_pandas(df, 2)
gdf = cudf.from_pandas(df)
gddf = dask_cudf.from_cudf(gdf, 2)
dd.assert_eq(
ddf.groupby("a").b.unique().compute(),
gddf.groupby("a").b.unique().compute(),
)
rjzamora marked this conversation as resolved.
Show resolved Hide resolved

dd.assert_eq(
gdf.groupby("a").b.unique(),
gddf.groupby("a").b.unique().compute(),
Expand Down
Loading