Use Index._column in favor of Index._values - #15494
Conversation
| @cached_property # type: ignore | ||
| @_cudf_nvtx_annotate | ||
| def _values(self): | ||
| def _column(self): |
There was a problem hiding this comment.
This change is somewhat conceptually problematic. A RangeIndex isn't backed by a column. My hope was to be able to rewrite algorithms in cudf to never rely on this kind of method at all, so as to be agnostic to the type of index and not accidentally materialize GPU data (see #9593). Should we prioritize those changes rather than making a cosmetic improvement like this one that is likely to only increase the frequency with which we accidentally allocate device memory?
There was a problem hiding this comment.
An effort to remove BaseIndex._values entirely as described in #9593 seems like a more worthy undertaking.
Curious how you envisioned intentionally materializing a RangeIndex to a column? as_column?
There was a problem hiding this comment.
I think a good starting point would be removing the ability to materialize a RangeIndex (i.e. removing those code paths from as_column and methods like RangeIndex._values altogether) and then seeing what breaks. In an ideal world we would implement versions of our code that avoid materialization altogether.
There was a problem hiding this comment.
OK some initial operations that would break:
.reset_indexwhere the index is aRangeIndexi.e. theRangeIndexneeds to materialized to a new columnRangeIndex.equalse.g. anIndex[int64]canequalsaRangeIndex. (I suppose materialization can be avoided by checking if anIndexcan be "compressed" to arange)RangeIndex.astypeto a nonint64type
There was a problem hiding this comment.
- My gut reaction is that this seems wrong. If the index is a
RangeIndex, then the output ofreset_indexshould also be aRangeIndex. pandas bears this out
>>> df = pd.DataFrame({'a': [1, 2, 3]})
>>> df.index
RangeIndex(start=0, stop=3, step=1)
>>> df.reset_index().index
RangeIndex(start=0, stop=3, step=1)
where does it break for us?
2. I think figuring out whether an integer index can be "compressed" to a range is going to be more work to implement than it's worth. To do that without allocating any intermediate memory for the diffs would require a slightly nontrivial algorithm to do something like compute the steps between every pair of elements, then do some synchronizations of a local variable a la a reduction. A simpler approach that would still require some GPU code but would be conceptually very straightforward would be to implement a libcudf function that allows you to check whether a column is equal to a range by specifying the range as a smart iterator, e.g. a counting iterator from start to stop with the step. That would be almost trivial to implement in C++. I don't think it's worth doing this right now since there would be some additional work to make that available in Python and then branch accordingly in Column.equals instead of calling a binop, but maybe we open an issue with the suggestion and leave it as a good first issue for libcudf (unless you're interested in tackling it yourself!).
3. We obviously can't avoid materializing in this case, but can we change how we handle this? For example, here's a one-liner that gets us what we need and would allow us to never need to use RangeIndex._values:
>>> df = pd.DataFrame({'a': [1, 2, 3]})
>>> df.index
RangeIndex(start=0, stop=3, step=1)
>>> cp.asarray(df.index, dtype=np.int32)
array([0, 1, 2], dtype=int32)
xref #15494 If the attributes are exposed on the top level object e.g. `Index.dtype` it should be sufficient to just access the attributes there instead of reaching for the underlying object Authors: - Matthew Roeschke (https://github.com/mroeschke) Approvers: - Vyas Ramasubramani (https://github.com/vyasr) URL: #15624
xref #15494 * For `Index.str`, check the `dtype` instead of the underlying column type (which would materialize RangeIndex) * For `set_index`, don't immediately convert passed objects to column until necessary * For `_make_operands_and_index_for_binop`, don't create pandas object more than once Authors: - Matthew Roeschke (https://github.com/mroeschke) - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - GALI PREM SAGAR (https://github.com/galipremsagar) URL: #15763
|
@mroeschke should we close this PR? Is it useful for tracking purposes? IIRC based on the main discussion above we're going in a different direction altogether to try and reduce materializations. |
|
It's useful for tracking but it doesn't need to remain open so I'll close this |
Description
The base definition is
So I think it's just more straightforward to use
self._columnsChecklist