Repository navigation
cudf-polars: preserve zero-column row counts #23234
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
Changes from 1 commit
4589c64
8b6a497
b67c449
958cb32
ab5995a
4027205
1faba5a
ca8bbe5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,7 +93,6 @@ class DataFrame: | |
| table: plc.Table | ||
| columns: list[NamedColumn] | ||
| stream: Stream | ||
| _num_rows_override: int | None | ||
|
|
||
| def __init__( | ||
| self, columns: Iterable[Column], stream: Stream, num_rows: int | None = None | ||
|
|
@@ -104,24 +103,25 @@ def __init__( | |
| self.columns = [cast("NamedColumn", c) for c in columns] | ||
| self.dtypes = [c.dtype for c in self.columns] | ||
| self.column_map = {c.name: c for c in self.columns} | ||
| self.table = plc.Table([c.obj for c in self.columns]) | ||
| self.table = plc.Table([c.obj for c in self.columns], num_rows=num_rows) | ||
| self.stream = stream | ||
| self._num_rows_override = num_rows | ||
|
|
||
| def copy(self) -> Self: | ||
| """Return a shallow copy of self.""" | ||
| return type(self)( | ||
| (c.copy() for c in self.columns), | ||
| stream=self.stream, | ||
| num_rows=self._num_rows_override, | ||
| num_rows=self.num_rows, | ||
| ) | ||
|
|
||
| def to_polars(self) -> pl.DataFrame: | ||
| """Convert to a polars DataFrame.""" | ||
| if self._num_rows_override is not None and len(self.column_map) == 0: | ||
| if len(self.column_map) == 0: | ||
| # polars < 1.38 has no DataFrame(height=...) constructor and cannot | ||
| # represent a zero-column frame with a non-zero row count. | ||
| if POLARS_VERSION_LT_138: # pragma: no cover | ||
| return pl.DataFrame() | ||
| return pl.DataFrame(height=self._num_rows_override) | ||
| return pl.DataFrame(height=self.num_rows) | ||
|
|
||
| # If the arrow table has empty names, from_arrow produces | ||
| # column_$i. But here we know there is only one such column | ||
|
|
@@ -163,8 +163,6 @@ def num_columns(self) -> int: | |
| @cached_property | ||
| def num_rows(self) -> int: | ||
| """Number of rows.""" | ||
| if self._num_rows_override is not None: | ||
| return self._num_rows_override | ||
| return self.table.num_rows() | ||
|
|
||
| @classmethod | ||
|
|
@@ -195,6 +193,7 @@ def from_polars(cls, df: pl.DataFrame, stream: Stream) -> Self: | |
| ) | ||
| ), | ||
| stream=stream, | ||
| num_rows=plc_table.num_rows(), | ||
| ) | ||
|
|
||
| @classmethod | ||
|
|
@@ -244,7 +243,7 @@ def from_table( | |
| for c, name, dtype in zip(table.columns(), names, dtypes, strict=True) | ||
| ), | ||
| stream=stream, | ||
| num_rows=num_rows, | ||
| num_rows=table.num_rows() if num_rows is None else num_rows, | ||
| ) | ||
|
|
||
| @classmethod | ||
|
|
@@ -285,6 +284,8 @@ def deserialize( | |
| for c, kw in zip(table.columns(), header["columns_kwargs"], strict=True) | ||
| ), | ||
| stream=stream, | ||
| # A zero-column frame's row count is carried by the packed metadata; preserve it. | ||
| num_rows=table.num_rows(), | ||
| ) | ||
|
|
||
| def serialize( | ||
|
|
@@ -358,6 +359,7 @@ def sorted_like( | |
| for c, other in zip(self.columns, like.columns, strict=True) | ||
| ), | ||
| stream=self.stream, | ||
| num_rows=self.num_rows, | ||
| ) | ||
|
|
||
| def with_columns( | ||
|
|
@@ -396,20 +398,31 @@ def with_columns( | |
| new = {c.name: c for c in columns} | ||
| if replace_only and not self.column_names_set.issuperset(new.keys()): | ||
| raise ValueError("Cannot replace with non-existing names") | ||
| return type(self)((self.column_map | new).values(), stream=stream) | ||
| merged = self.column_map | new | ||
| # When the result has columns, the row count is derived from them (and the | ||
| # non-broadcast path deliberately allows mismatched lengths, so we must not | ||
| # force a num_rows here). | ||
| return type(self)( | ||
| merged.values(), | ||
| stream=stream, | ||
| num_rows=self.num_rows if not merged else None, | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is invalid to pass new columns that have a different length from the existing table. So what is this comment saying?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not always, updated the comment: # Only pass num_rows for a zero-column result. For results with columns, it
# must remain None because HStack(should_broadcast=False) intentionally
# produces mismatched column lengths that its Select parent reconciles later.
return type(self)(
merged.values(),
stream=stream,
num_rows=self.num_rows if not merged else None,
) |
||
|
|
||
| def discard_columns(self, names: Set[str]) -> Self: | ||
| """Drop columns by name.""" | ||
| return type(self)( | ||
| (column for column in self.columns if column.name not in names), | ||
| stream=self.stream, | ||
| num_rows=self.num_rows, | ||
| ) | ||
|
|
||
| def select(self, names: Sequence[str] | Mapping[str, Any]) -> Self: | ||
| """Select columns by name returning DataFrame.""" | ||
| try: | ||
| return type(self)( | ||
| (self.column_map[name] for name in names), stream=self.stream | ||
| (self.column_map[name] for name in names), | ||
| stream=self.stream, | ||
| num_rows=self.num_rows, | ||
| ) | ||
| except KeyError as e: | ||
| raise ValueError("Can't select missing names") from e | ||
|
|
@@ -419,6 +432,7 @@ def rename_columns(self, mapping: Mapping[str, str]) -> Self: | |
| return type(self)( | ||
| (c.rename(mapping.get(c.name, c.name)) for c in self.columns), | ||
| stream=self.stream, | ||
| num_rows=self.num_rows, | ||
| ) | ||
|
|
||
| def select_columns(self, names: Set[str]) -> list[Column]: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Now that
pylibcudf.Tableaccepts anum_rowsargument, would be good to see if the we can removenum_rowsfrom this function and have the caller always pass in atablewith the intended row countThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, done: ab5995a