Skip to content

feat: implement pivot for the pyarrow backend - #3816

Open
mkzung wants to merge 2 commits into
narwhals-dev:mainfrom
mkzung:feat/arrow-pivot
Open

feat: implement pivot for the pyarrow backend#3816
mkzung wants to merge 2 commits into
narwhals-dev:mainfrom
mkzung:feat/arrow-pivot

Conversation

@mkzung

@mkzung mkzung commented Jul 24, 2026

Copy link
Copy Markdown

Description

Implements pivot for the pyarrow backend (it was not_implemented).

pyarrow has no native pivot, so it groups by index + on, then scatters each aggregated
cell into its output row and column. Cells are matched on Python tuples, so it stays correct
when index or on hold nulls (a pyarrow join cannot match null keys).

It follows polars for column names ({"a","b"} for multiple on, a {value}{separator}
prefix for multiple values, true/false/null for booleans and nulls), column order
(occurring on combinations by first appearance, or sorted), empty-cell fill (0 for sum
and len, null otherwise), null handling in first/last, and the "not unique" error for
aggregate_function=None.

median maps to pyarrow's approximate hash median (there is no exact one), as
group_by(...).agg(median()) already does, so its exact-value test stays xfail for pyarrow.

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • 🔧 Optimization
  • 📝 Documentation
  • ✅ Test
  • 🐳 Other

Related issues

AI assistance

  • No AI tools were used for this PR.
  • AI tools were used.

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

Closes narwhals-dev#2179. ArrowDataFrame.pivot was not_implemented. pyarrow has no native
pivot, so this groups by index + on, then scatters each aggregated cell into its
output row and column, matched on Python tuples so it stays correct when index or
on hold nulls (a pyarrow join cannot match null keys). It follows polars for
column names, order, null handling, and the empty-cell fill. median maps to
pyarrow's approximate hash median, so its exact-value test stays xfail for pyarrow
(same as group_by median).
@MarcoGorelli MarcoGorelli added the enhancement New feature or request label Jul 24, 2026
@FBruzzesi FBruzzesi added the pyarrow Issue is related to pyarrow backend label Jul 25, 2026
@FBruzzesi FBruzzesi mentioned this pull request Jul 26, 2026
7 tasks
first/last are ordered aggregators: pyarrow computes them only in
single-threaded execution (use_threads=False), which Table.group_by does
not accept before 14.0. Pass the flag only when it is needed and available,
mirroring _configure_grouped in group_by.py, and xfail the ordered pivot
cases on pyarrow<14.

Make _pivot_reshape's schema arguments keyword-only so it stays under the
positional-argument limit (PLR0917).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request pyarrow Issue is related to pyarrow backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enh]: Add pivot to Arrow

3 participants