Skip to content

Closes #4993: ArkoudaArray.argsort - #4999

Merged
ajpotts merged 1 commit into
Bears-R-Us:mainfrom
ajpotts:4993_ArkoudaArray.argsort
Oct 20, 2025
Merged

ajpotts merged 1 commit into
Bears-R-Us:mainfrom
ajpotts:4993_ArkoudaArray.argsort

Conversation

@ajpotts

@ajpotts ajpotts commented Oct 15, 2025 •

Copy link
Copy Markdown
Contributor

Add argsort to ArkoudaExtensionArray

Implements the ExtensionArray.argsort
API for Arkouda-backed arrays.

Summary

  • Adds a fully pandas-compatible argsort implementation to ArkoudaBaseArray.
  • Supports pdarray, Strings, and Categorical types.
  • Performs sorting on the Arkouda server for efficiency.
  • Returns a pdarray, and uses a # type: ignore[override] to ignore pandas and mypy type expectations. Pandas expects a NumPy ndarray to be returned.
  • Handles ascending, na_position, and kind arguments for API compatibility.

Implementation Details

  • Uses Arkouda’s internal argsort to compute the permutation remotely.
  • Adjusts NaN placement based on na_position ("first" or "last").
  • Raises ValueError for invalid na_position values.
  • Raises TypeError for unsupported underlying dtypes.
  • Returns a pdarray, contrary to pandas expectation to return a NumPy ndarray.

Tests Added

test_argsort_extensionarray.py includes:

  • Float pdarray with NaN handling (ascending / descending, first / last).
  • Strings and Categorical sorting correctness.
  • Error handling for invalid na_position and unsupported dtypes.
  • Ensures kind and extra kwargs are accepted but ignored.

Example

>>> import arkouda as ak
>>> from arkouda.pandas.extension import ArkoudaArray

>>> a = ArkoudaArray(ak.array([3.0, float("nan"), 1.0]))
>>> a.argsort()  # NaN last by default
array([2 0 1])

>>> a.argsort(na_position="first")
array([1 2 0])

Why This Matters

This addition brings Arkouda’s pandas integration closer to parity with native ExtensionArray behavior.
It enables pandas operations such as sorting and reindexing to function correctly on Arkouda-backed columns,
while keeping heavy data resident on the server for performance and scalability.

Closes #4993: ArkoudaArray.argsort

@ajpotts
ajpotts force-pushed the 4993_ArkoudaArray.argsort branch from 6b356b2 to 35832f0 Compare October 15, 2025 16:25
@ajpotts ajpotts added the blocking This is blocking a developer from completing a task they are actively working. label Oct 15, 2025
@ajpotts
ajpotts marked this pull request as ready for review October 15, 2025 17:51
@1RyanK

1RyanK commented Oct 16, 2025 •

Copy link
Copy Markdown
Contributor

Does it matter if Categorical adjusts for NA values?

import arkouda as ak
from arkouda.pandas.categorical import Categorical
from arkouda.pandas.extension._arkouda_base_array import ArkoudaBaseArray

# ak.connect(server="localhost", port=5555)  # if not already connected

# Step 1: build a categorical with valid codes
vals = ak.array(["b", "a", "c", "b"])
cat = Categorical(vals)

# Step 2: mutate a couple of codes to NA (use categories.size as the NA sentinel)
print("original codes:", cat.codes)
cat.codes[0] = cat.categories.size - 1  # first element -> NA
cat.codes[2] = cat.categories.size - 1  # third element -> NA
print("with NAs codes:", cat.codes)

# Optional: verify NA mask if available
if hasattr(cat, "isna"):
    print("NA mask:", cat.isna())

# Step 3: wrap in extension array
ea = ArkoudaBaseArray(cat)

# Step 4: compare argsort permutations
perm_last  = ea.argsort(ascending=True,  na_position="last")
perm_first = ea.argsort(ascending=True,  na_position="first")

print("perm_last :", perm_last)
print("perm_first:", perm_first)
print("identical?:", ak.all(perm_last == perm_first))

# Step 5: see the sorted values for reference
print("values (asc, last): ", cat[perm_last])
print("values (asc, first):", cat[perm_first])

I feel like the docstring should mention that na_position only works for pdarray, and maybe put up an issue for that for now?

@ajpotts
ajpotts force-pushed the 4993_ArkoudaArray.argsort branch from 35832f0 to 8db6b73 Compare October 16, 2025 18:10
@ajpotts

ajpotts commented Oct 16, 2025

Copy link
Copy Markdown
Contributor Author

That's fair. Pandas has it's own way NaN handling, so to implement it properly would merit a separate PR. I updated the docstring and made a ticket for it: #5006

@1RyanK 1RyanK left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good!

@ajpotts
ajpotts force-pushed the 4993_ArkoudaArray.argsort branch 2 times, most recently from 30f7268 to 3ef014b Compare October 17, 2025 18:31
@ajpotts
ajpotts force-pushed the 4993_ArkoudaArray.argsort branch from 3ef014b to 9ea74c3 Compare October 17, 2025 19:00

@jaketrookman jaketrookman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good

@ajpotts
ajpotts added this pull request to the merge queue Oct 20, 2025
Merged via the queue into Bears-R-Us:main with commit 5bafdb4 Oct 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocking This is blocking a developer from completing a task they are actively working.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ArkoudaArray.argsort

3 participants