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

Access Frame attributes instead of ColumnAccessor attributes when available #16652

Merged
merged 13 commits into from
Sep 19, 2024

Conversation

mroeschke
Copy link
Contributor

Description

There are some places where a public object like DataFrame or Index accesses a ColumnAccessor attribute when it's accessible in a shared subclass attribute instead (like Frame).

In an effort to access the ColumnAccessor less, replaced usages of ._data.attribute with a Frame specific attribute`

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@mroeschke mroeschke added Python Affects Python cuDF API. improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels Aug 23, 2024
@mroeschke mroeschke requested a review from a team as a code owner August 23, 2024 23:23
rapids-bot bot pushed a commit that referenced this pull request Aug 28, 2024
Discovered in #16652, `DataFrame.iloc/loc.__setitem__` with a non-cupy type e.g. `"category"` failed because the indexing path unconditionally tries to `cupy.asarray` the value to be set which only accepts types recognized by cupy.

We can skip this `asarray` if we have a numpy/pandas/cudf object

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

URL: #16677
@@ -273,8 +273,7 @@ def read_csv(
elif isinstance(dtype, abc.Collection):
for index, col_dtype in enumerate(dtype):
if isinstance(cudf.dtype(col_dtype), cudf.CategoricalDtype):
col_name = df._data.names[index]
df._data[col_name] = df._data[col_name].astype(col_dtype)
df.iloc[:, index] = df.iloc[:, index].astype(col_dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this change not end up being expensive since we are touching the public dataframe API + typecasting a series which will be having the returning the index too?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, this is quite a lot more expensive. We're replacing two dict lookups with quite a lot of work to determine how to do those two dict lookups.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's fair. I'll revert this change

Copy link
Contributor

@wence- wence- left a comment

Choose a reason for hiding this comment

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

Some very small nits, in addition to @galipremsagar's comment.

@@ -273,8 +273,7 @@ def read_csv(
elif isinstance(dtype, abc.Collection):
for index, col_dtype in enumerate(dtype):
if isinstance(cudf.dtype(col_dtype), cudf.CategoricalDtype):
col_name = df._data.names[index]
df._data[col_name] = df._data[col_name].astype(col_dtype)
df.iloc[:, index] = df.iloc[:, index].astype(col_dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, this is quite a lot more expensive. We're replacing two dict lookups with quite a lot of work to determine how to do those two dict lookups.

return zip(self._column_names, self._columns)

@property
def _dtypes(self) -> abc.Generator:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def _dtypes(self) -> abc.Generator:
def _dtypes(self) -> abc.Generator[tuple[Hashable, Dtype], None, None]:

? can't remember what the type of col.dtype is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Yup it's Dtype

@@ -75,8 +75,13 @@ def _columns(self) -> tuple[ColumnBase, ...]:
return self._data.columns

@property
def _dtypes(self) -> abc.Iterable:
return zip(self._data.names, (col.dtype for col in self._data.columns))
def _column_labels_and_values(self) -> abc.Iterable:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def _column_labels_and_values(self) -> abc.Iterable:
def _column_labels_and_values(self) -> abc.Iterable[tuple[Hashable, Dtype]]:

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The second argument in the tuple should be ColumnBase, but thanks!

@galipremsagar
Copy link
Contributor

/merge

@rapids-bot rapids-bot bot merged commit d63ca6a into rapidsai:branch-24.10 Sep 19, 2024
94 checks passed
rjzamora pushed a commit to rjzamora/cudf that referenced this pull request Sep 24, 2024
…ilable (rapidsai#16652)

There are some places where a public object like `DataFrame` or `Index` accesses a `ColumnAccessor` attribute when it's accessible in a shared subclass attribute instead (like `Frame`).

In an effort to access the `ColumnAccessor` less, replaced usages of `._data.attribute` with a `Frame` specific attribute`

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

URL: rapidsai#16652
@mroeschke mroeschke deleted the ref/frame/attributes branch September 25, 2024 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improvement / enhancement to an existing function non-breaking Non-breaking change Python Affects Python cuDF API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants