-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF: Series(pyarrow-backed).rank #50264
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 3 commits
f7a21c2
743d90a
708878e
85226f5
5d1a533
dee82ee
6ba998e
a648a90
bd90163
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
from pandas._typing import ( | ||
ArrayLike, | ||
AxisInt, | ||
Dtype, | ||
FillnaOptions, | ||
Iterator, | ||
|
@@ -22,6 +23,7 @@ | |
from pandas.compat import ( | ||
pa_version_under6p0, | ||
pa_version_under7p0, | ||
pa_version_under9p0, | ||
) | ||
from pandas.util._decorators import doc | ||
from pandas.util._validators import validate_fillna_kwargs | ||
|
@@ -949,7 +951,68 @@ def _indexing_key_to_indices( | |
indices = np.arange(n)[key] | ||
return indices | ||
|
||
# TODO: redefine _rank using pc.rank with pyarrow 9.0 | ||
def _rank( | ||
self: ArrowExtensionArrayT, | ||
*, | ||
axis: AxisInt = 0, | ||
method: str = "average", | ||
na_option: str = "keep", | ||
ascending: bool = True, | ||
pct: bool = False, | ||
) -> ArrowExtensionArrayT: | ||
""" | ||
See Series.rank.__doc__. | ||
""" | ||
if axis != 0: | ||
raise NotImplementedError | ||
|
||
if ( | ||
pa_version_under9p0 | ||
# as of version 10, pyarrow does not support an "average" method | ||
or method not in ("min", "max", "first", "dense") | ||
): | ||
from pandas.core.algorithms import rank | ||
|
||
ranked = rank( | ||
self.to_numpy(), | ||
axis=axis, | ||
method=method, | ||
na_option=na_option, | ||
ascending=ascending, | ||
pct=pct, | ||
) | ||
result = pa.array(ranked, type=pa.float64(), from_pandas=True) | ||
return type(self)(result) | ||
|
||
sort_keys = "ascending" if ascending else "descending" | ||
|
||
if na_option == "top": | ||
null_placement = "at_start" | ||
else: | ||
null_placement = "at_end" | ||
|
||
result = pc.rank( | ||
self._data.combine_chunks(), | ||
sort_keys=sort_keys, | ||
null_placement=null_placement, | ||
tiebreaker=method, | ||
) | ||
if not pa.types.is_floating(result.type): | ||
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. Is this because the existing rank implementation only returns 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. [updated] - my original comment was inaccurate. I just changed this to let the pyarrow behavior flow through which is 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. Right, but if I'm suggesting we just keep the 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. Let me know if that's what you had in mind. 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 our comments got crossed and were now on the same page? |
||
result = result.cast(pa.float64()) | ||
|
||
if na_option == "keep": | ||
mask = pc.is_null(self._data) | ||
null = pa.scalar(None, type=self._data.type) | ||
result = pc.if_else(mask, null, result) | ||
|
||
if pct: | ||
if method == "dense": | ||
divisor = pc.max(result) | ||
else: | ||
divisor = pc.count(result) | ||
result = pc.divide(result, divisor) | ||
|
||
return type(self)(result) | ||
|
||
def _quantile( | ||
self: ArrowExtensionArrayT, qs: npt.NDArray[np.float64], interpolation: str | ||
|
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.
Can't
ranked = super().rank(...)
because of theto_numpy()
call? It seems that prior it works ifself
is passed?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.
Yes, we could. However, I observed this:
Probably worth a look in
algos.rank
to see whats going on. Ok to do as a separate PR or should I look at that first?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.
Follow up PR is good. Ideally it would be good to have the
axis != 1
case fall through to super and raise there