Skip to content

Support na_position in ArkoudaArray.argsort for Strings and Categorical #5006

Open

Description

@ajpotts

Summary

Extend ArkoudaArray.argsort so that the na_position argument works
correctly for String and Categorical-backed extension arrays.

Currently, na_position is only supported when the underlying data is a
pdarray (numeric types). For Strings and Categorical dtypes, behavior
is unsupported or inconsistent with pandas semantics.

This ticket ensures pandas-compatible sorting behavior across all
Arkouda-backed ExtensionArray types.


Background

In pandas, Series.sort_values() delegates to
ExtensionArray.argsort() and passes the na_position argument:

  • "first" → missing values appear at the beginning
  • "last" → missing values appear at the end

For Arkouda-backed arrays:

  • Numeric (pdarray) types correctly support na_position
  • String-backed arrays do not correctly reposition missing values
  • Categorical-backed arrays do not correctly reposition missing values

This results in inconsistent sorting behavior depending on dtype.


Expected pandas Semantics

For an array:

["b", None, "a"]
  • na_position="last" → ["a", "b", None]
  • na_position="first" → [None, "a", "b"]

Sorting rules:

  1. Non-missing values are sorted according to dtype ordering.
  2. Missing values are grouped entirely at the beginning or end.
  3. Relative ordering of equal elements should match pandas
    expectations.

The same semantics apply to categorical arrays with missing categories.


Scope

In Scope

  • Update ArkoudaArray.argsort to support na_position for:
    • String-backed arrays
    • Categorical-backed arrays
  • Maintain existing numeric (pdarray) behavior
  • Ensure compatibility with:
    • Series.sort_values()
    • DataFrame.sort_values()
  • Add regression tests

Out of Scope

  • Multi-column sorting (handled by pandas)
  • Large-scale performance optimization beyond correctness
  • Internal redesign of String/Categorical storage

Implementation Considerations

1. Detect Missing Values

  • Extract a boolean mask identifying NA positions.
  • Ensure mask detection is consistent across String and Categorical
    types.

2. Sort Non-Missing Values

  • Use existing Arkouda sorting primitives for Strings and Categorical.
  • Perform sorting only on non-missing values.

3. Combine Indices

Depending on na_position:

  • "last" → concatenate [sorted_non_na_indices, na_indices]
  • "first" → concatenate [na_indices, sorted_non_na_indices]

4. Stability

  • Confirm whether Arkouda string/categorical sort is stable.
  • If not, document behavior or adjust implementation to match pandas
    where feasible.

5. Error Handling

  • Invalid na_position → raise ValueError
  • Unsupported dtype → raise NotImplementedError

Example Test Skeleton

>>> import arkouda as ak
>>> import pandas as pd

>>> s = pd.Series(["b", None, "a"], dtype="ak")
>>> s.sort_values(na_position="first").tolist()
[None, "a", "b"]

>>> s.sort_values(na_position="last").tolist()
["a", "b", None]

Add equivalent tests for categorical-backed Series.

Activity

  1. changed the title [-]ArkoudaArray.argsort na_position argument to work for Strings and Categorical[/-] [+]Support `na_position` in `ArkoudaArray.argsort` for Strings and Categorical[/+] on Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions