Skip to content

Closes #5074: create arkouda accessor for pandas Index - #5075

Merged
ajpotts merged 2 commits into
Bears-R-Us:mainfrom
ajpotts:5074_create_arkouda_accessor_for_pandas_Index
Dec 4, 2025
Merged

ajpotts merged 2 commits into
Bears-R-Us:mainfrom
ajpotts:5074_create_arkouda_accessor_for_pandas_Index

Conversation

@ajpotts

@ajpotts ajpotts commented Nov 19, 2025 •

Copy link
Copy Markdown
Contributor

Summary of Changes

New module:

arkouda/pandas/extension/_index_accessor.py

This module defines:

  1. .ak Index/MultiIndex accessor (ArkoudaIndexAccessor)
    Provides the following methods:
  • .to_ak()
    Convert a pandas Index/MultiIndex to an Arkouda-backed pandas object
    using ArkoudaExtensionArray. Zero NumPy materialization.

  • .collect()

Materialize Arkouda-backed index data into NumPy-backed pandas objects.
Opposite of .to_ak().

  • .to_ak_legacy()

Produce true legacy Arkouda ak.Index / ak.MultiIndex objects.

  • .from_ak_legacy(akidx)

Wrap legacy Arkouda index objects back into Arkouda-backed pandas objects.

  • .is_arkouda

Boolean property indicating whether the index is backed by Arkouda.

  1. Internal helpers
  • _pandas_index_to_ak

  • _ak_index_to_pandas_no_copy

These implement the zero-copy transformation between pandas EAs and Arkouda objects.

Closes #5074: create arkouda accessor for pandas Index

@ajpotts
ajpotts force-pushed the 5074_create_arkouda_accessor_for_pandas_Index branch from f4789d3 to 7198dcc Compare November 19, 2025 21:15
@ajpotts
ajpotts force-pushed the 5074_create_arkouda_accessor_for_pandas_Index branch from 7198dcc to c6f8bca Compare December 2, 2025 14:34
@ajpotts
ajpotts force-pushed the 5074_create_arkouda_accessor_for_pandas_Index branch from c6f8bca to 7c728ed Compare December 2, 2025 14:35
@ajpotts
ajpotts marked this pull request as ready for review December 2, 2025 14:37

@drculhane drculhane 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.

Again, looks comprehensive. Nicely done.

@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 Dec 4, 2025

@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, I think a few minor things though

pandas index.

This is the opposite of :meth:`to_ak`. If the index is already
NumPy-backed, this is effectively a shallow copy.

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.

I think it's not quite a shallow copy?

import pandas as pd

# This is a plain, normal pandas MultiIndex
arrays = [[1, 1, 2], ["red", "blue", "red"]]
midx = pd.MultiIndex.from_arrays(arrays, names=["num", "color"])

print("Original MultiIndex:")
print(midx)
print("Original length:", len(midx))

# This uses your ArkoudaIndexAccessor.collect() implementation
midx2 = midx.ak.collect()

print("\nAfter midx.ak.collect():")
print(midx2)
print("Collected length:", len(midx2))
print("Equals original?", midx2.equals(midx))

Output is

Original MultiIndex:
MultiIndex([(1,  'red'),
            (1, 'blue'),
            (2,  'red')],
           names=['num', 'color'])
Original length: 3

After midx.ak.collect():
MultiIndex([(1, 'blue'),
            (2,  'red')],
           names=['num', 'color'])
Collected length: 2
Equals original? False

"""
if hasattr(self._obj, "levels"):
for level in self._obj.levels:
values = level._data if hasattr(level, "_data") else None

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.

Would it be better to use .array instead of ._data?

# pandas.MultiIndex in its constructor.
if isinstance(index, pd.MultiIndex):
# Preserve names when constructing the ak.MultiIndex
return ak_MultiIndex(index, names=[str(n) for n in index.names])

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.

I think this does not quite match pandas behavior:

import pandas as pd
import arkouda as ak
from arkouda.pandas.extension._index_accessor import _pandas_index_to_ak

arrays = [[1, 1], [2, 3]]

# Note: first name is None, second is a string
midx = pd.MultiIndex.from_arrays(arrays, names=[None, "b"])

print("Original pandas MultiIndex names:", list(midx.names))

ak_midx = _pandas_index_to_ak(midx)

print("Arkouda MultiIndex names:", list(ak_midx.names))
print("Raw names repr:", repr(list(ak_midx.names)))

Output is

Original pandas MultiIndex names: [None, 'b']
Arkouda MultiIndex names: ['None', 'b']
Raw names repr: ['None', 'b']

Note that in pandas, it is literally None and you have turned it into a string


The resulting index stores its values on the Arkouda server:

>>> type(pd_idx._data)

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.

I think .array also works well here and avoids private variables

Merged via the queue into Bears-R-Us:main with commit b11ecc4 Dec 4, 2025
github-merge-queue Bot pushed a commit that referenced this pull request Dec 17, 2025
# Add `.ak` Series Accessor & Improve EA Construction

This PR introduces a new `.ak` accessor for `pandas.Series`, completing
the pandas-side API for Arkouda-backed objects and bringing Series to
parity with the existing Index/MultiIndex and DataFrame accessors.
## Summary of Changes

### 1. Add `ArkoudaSeriesAccessor` (`pd.Series.ak`)
New module: `arkouda/pandas/extension/_series_accessor.py`

This accessor enables seamless, zero-copy transitions between:

- **NumPy-backed pandas Series**
- **pandas Series backed by ArkoudaExtensionArray** (distributed /
server-side)
- **legacy Arkouda arrays** (`pdarray`, `Strings`, `Categorical`)

Supported API:
- `s.ak.to_ak()` – Convert a Series to an Arkouda-backed Series while
preserving index and name
- `s.ak.collect()` – Materialize an Arkouda-backed Series back to NumPy
- `s.ak.to_ak_legacy()` – Produce the actual Arkouda array object  
- `pd.Series.ak.from_ak_legacy()` – Wrap a legacy Arkouda array in a
Series without materializing
- `s.ak.is_arkouda` – Introspection flag  

The implementation follows the same design, style, and semantics as the
existing Index and DataFrame accessors.

---

### 2. Register accessor and expose it
`ArkoudaSeriesAccessor` is now imported and exported from
`arkouda.pandas.extension.__init__`, making it part of the public API.

---

### 3. Improve `ArkoudaExtensionArray._from_sequence`
This new implementation adds a dtype-dispatching factory that:

- Detects Arkouda server-side arrays (`pdarray`, `Strings`,
`Categorical`)
- Returns the correct EA subclass (`ArkoudaArray`, `ArkoudaStringArray`,
`ArkoudaCategoricalArray`)
- Falls back to converting Python/NumPy sequences into Arkouda `pdarray`
(via `ak_array`)
- Eliminates several circular import hazards with scoped local imports
- Aligns behavior with pandas’ EA construction guidelines

This method is required for correct Series construction and zero-copy
wrapping in the accessor.

---

### 4. Add comprehensive unit tests
New file: `tests/pandas/extension/series_accessor.py`

Tests cover:

- Doctest validation of the accessor module  
- Numeric and string roundtrip conversions  
- Preservation of index values and **index names**  
- `_pandas_series_to_ak_legacy` and `_ak_legacy_to_pandas_series`  
- Idempotency of `to_ak()`  
- Proper type dispatch (`Strings` vs `pdarray`)  
- Correct usage of ArkoudaExtensionArray  

These tests mirror the Index accessor test style to maintain
consistency.

---

## Motivation

Arkouda’s pandas integration already supports (PRs pending
#5075 and
#5071):

- DataFrame `.ak` accessor  
- Index / MultiIndex `.ak` accessor  
- ExtensionArray subclasses  

Series was the missing piece. This PR completes the pandas API surface
and unlocks:

- Zero-copy Series-level conversions  
- Smooth DataFrame ↔ Series workflows  
- Easier construction of Arkouda-backed Series for testing and
prototyping

---

## User-Facing Impact

Users can now write:

```python
>>> s = pd.Series([1, 2, 3])
>>> s_ak = s.ak.to_ak()
>>> s_ak.ak.is_arkouda
True
```

Closes #5087:  pandas series accessor for arkouda

---------

Co-authored-by: ajpotts <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create arkouda accessor for pandas Index

4 participants