Skip to content

Avoid pylibcudf.interop.to_arrow in DataFrame.to_polars in cudf_polars #19198

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

Merged
merged 3 commits into from
Jun 25, 2025
Merged
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
20 changes: 17 additions & 3 deletions python/cudf_polars/cudf_polars/containers/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
if TYPE_CHECKING:
from collections.abc import Iterable, Mapping, Sequence, Set

from typing_extensions import Any, Self
from typing_extensions import Any, CapsuleType, Self

from cudf_polars.typing import ColumnOptions, DataFrameHeader, Slice

Expand All @@ -40,6 +40,20 @@ def _create_polars_column_metadata(
return plc.interop.ColumnMetadata(name=name, children_meta=children_meta)


# This is also defined in pylibcudf.interop
class _ObjectWithArrowMetadata:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think #18722 is the primary limitation that necessitates this workaround cc @vyasr

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, exactly. Without this you lose the underlying struct fields. This solution is fine for now while we sort out #18722.

def __init__(
self, obj: plc.Table, metadata: list[plc.interop.ColumnMetadata]
) -> None:
self.obj = obj
self.metadata = metadata

def __arrow_c_array__(
self, requested_schema: None = None
) -> tuple[CapsuleType, CapsuleType]:
return self.obj._to_schema(self.metadata), self.obj._to_host_array()


# Pacify the type checker. DataFrame init asserts that all the columns
# have a string name, so let's narrow the type.
class NamedColumn(Column):
Expand Down Expand Up @@ -82,8 +96,8 @@ def to_polars(self) -> pl.DataFrame:
)
for name, col in zip(name_map, self.columns, strict=True)
]
table = plc.interop.to_arrow(self.table, metadata=metadata)
df: pl.DataFrame = pl.from_arrow(table)
table_with_metadata = _ObjectWithArrowMetadata(self.table, metadata)
df = pl.DataFrame(table_with_metadata)
return df.rename(name_map).with_columns(
pl.col(c.name).set_sorted(descending=c.order == plc.types.Order.DESCENDING)
if c.is_sorted
Expand Down