Skip to content

Closes #4983: pandas dataframe accessor for arkouda - #5071

Merged
ajpotts merged 4 commits into
Bears-R-Us:mainfrom
ajpotts:4983_pandas_dataframe_accessor_for_arkouda
Dec 8, 2025
Merged

ajpotts merged 4 commits into
Bears-R-Us:mainfrom
ajpotts:4983_pandas_dataframe_accessor_for_arkouda

Conversation

@ajpotts

@ajpotts ajpotts commented Nov 17, 2025 •

Copy link
Copy Markdown
Contributor

PR: Add ArkoudaDataFrameAccessor and End-to-End DataFrame Support in Pandas Extension API

This PR introduces full DataFrame-level support for Arkouda-backed pandas workflows, enabling zero-copy conversions, seamless merging, and rich interoperability between pandas.DataFrame and arkouda.pandas.dataframe.DataFrame.

Summary of Changes

🚀 New ArkoudaDataFrameAccessor (df.ak)

A new pandas DataFrame accessor is added:

df.ak

This accessor provides:

  • df.ak.to_ak() — convert a pandas DataFrame to an Arkouda-backed pandas DataFrame (zero-copy-ish)
  • df.ak.collect() — materialize Arkouda-backed columns to NumPy locally
  • df.ak.to_ak_legacy() — convert to legacy ak.DataFrame
  • pd.DataFrame.ak.from_ak_legacy(akdf) — zero-copy conversion from legacy Arkouda DataFrame to pandas
  • df.ak.merge(...) — merge two Arkouda-backed pandas DataFrames using server-side Arkouda join

🔄 Conversion Routines

Internal helpers implemented and tested:

  • _looks_like_ak_col
  • _extract_ak_from_ea
  • _is_arkouda_series
  • _series_to_akcol_no_copy
  • _df_to_akdf_no_copy
  • _akdf_to_pandas_no_copy

These enable safe, zero-copy conversions in both directions.

🧪 Extensive Unit Tests

Added tests/pandas/extension/dataframe_accessor.py covering:

  • doctests
  • conversion round-trips
  • merge behavior (inner, left, right, outer)
  • error validation for non-Arkouda-backed inputs

Updated test discovery in pytest.ini.

Motivation

This PR completes the pandas ExtensionArray integration by providing first-class DataFrame operations. Prior to this work, users needed to manually convert via legacy ak.DataFrame APIs or operate at the Series/EA level. This PR removes that friction and enables:

  • idiomatic pandas usage (df.ak.merge(df2))
  • efficient zero-copy DataFrame wrapping
  • round-trippable interop between pandas and Arkouda
  • future support for distributed DataFrame operations

Example Usage

Convert to Arkouda-backed pandas DataFrame

import pandas as pd
import arkouda as ak

df = pd.DataFrame({"x": [1,2,3], "s": ["a","b","c"]})
akdf = df.ak.to_ak()

Merge using server-side Arkouda join

out = akdf1.ak.merge(akdf2, on="id", how="inner")

Convert a legacy ak.DataFrame to pandas (zero-copy)

pdf = pd.DataFrame.ak.from_ak_legacy(akdf)

Fully materialize back to NumPy

pdf_numpy = pdf.ak.collect()

Closes #4983: pandas dataframe accessor for arkouda

@ajpotts
ajpotts force-pushed the 4983_pandas_dataframe_accessor_for_arkouda branch 3 times, most recently from 1a168a3 to 8b49c67 Compare November 18, 2025 16:56
@ajpotts
ajpotts marked this pull request as ready for review November 18, 2025 18:45
@ajpotts
ajpotts force-pushed the 4983_pandas_dataframe_accessor_for_arkouda branch from 8b49c67 to 5edbc6f Compare November 18, 2025 23:05
@ajpotts
ajpotts marked this pull request as draft November 19, 2025 14:09
@ajpotts
ajpotts force-pushed the 4983_pandas_dataframe_accessor_for_arkouda branch 3 times, most recently from 772d566 to f9d58a4 Compare November 19, 2025 17:27
@ajpotts
ajpotts force-pushed the 4983_pandas_dataframe_accessor_for_arkouda branch 2 times, most recently from 4f7fc5e to 65dbd17 Compare December 2, 2025 17:57
@ajpotts
ajpotts marked this pull request as ready for review December 2, 2025 17:58
@ajpotts
ajpotts force-pushed the 4983_pandas_dataframe_accessor_for_arkouda branch from 02cdf3b to 62b9fa5 Compare December 3, 2025 13:42

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

It looks like you have some rebase lines hanging out in tests/pandas/extension/arkouda_extension.py, starting at line 567. Other than that, this looks good.

@ajpotts
ajpotts force-pushed the 4983_pandas_dataframe_accessor_for_arkouda branch from 94088f4 to 9eb7f5e Compare December 5, 2025 12:27

@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

@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 4983_pandas_dataframe_accessor_for_arkouda branch from 9eb7f5e to f22fa04 Compare December 8, 2025 17:46
@ajpotts
ajpotts added this pull request to the merge queue Dec 8, 2025
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Dec 8, 2025
@ajpotts
ajpotts merged commit 0b2d554 into Bears-R-Us:main Dec 8, 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.

pandas dataframe accessor for arkouda

4 participants