Skip to content

feat: add map.len() for Polars - #4182

Open
RahulGPi wants to merge 1 commit into
narwhals-dev:mainfrom
RahulGPi:feat/map-len
Open

RahulGPi wants to merge 1 commit into
narwhals-dev:mainfrom
RahulGPi:feat/map-len

Conversation

@RahulGPi

@RahulGPi RahulGPi commented Nov 19, 2026 •

Copy link
Copy Markdown

Description

This PR adds map.len() for expressions and Series backed by Polars 2.0 or newer. It counts the entries in each map, including entries with null values. Empty maps return 0, and null maps return null.

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.

Codex helped with implementation and tests. GPT-6.2 Astra reviewed the patch.

Checklist

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

Summary by CodeRabbit

  • New Features
    • Added map-length operations for series and expressions, returning the number of entries in each map as an unsigned 32-bit value. Null maps produce null results; null-valued entries are counted.
    • Map length is available with Polars 2.0 or newer.
  • Documentation
    • Updated the API reference to include map-length operations.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The PR adds map-length methods to the series and expression APIs. Polars 2.0 or newer delegates the operation to its native map accessor. Other listed backends mark the operation as unsupported.

Changes: Map length API and backend support

Layer / File(s) Summary
Public API and compliant expression path
src/narwhals/series_map.py, src/narwhals/expr_map.py, src/narwhals/_compliant/any_namespace.py, src/narwhals/_compliant/expr.py, docs/api-reference/series_map.md, docs/api-reference/expr_map.md
Series and expression map namespaces add len(). The compliant namespace declares the operation, and the eager expression path delegates it. The API references list the new methods.
Backend support and validation
src/narwhals/_polars/utils.py, src/narwhals/_arrow/series_map.py, src/narwhals/_duckdb/expr_map.py, src/narwhals/_ibis/expr_map.py, src/narwhals/_pandas_like/series_map.py, src/narwhals/_spark_like/expr_map.py, tests/expr_and_series/map/len_test.py
Polars checks for version 2.0 or newer before delegating map length to its native accessor. The other listed backends mark len as not implemented. Tests cover series, eager and lazy expressions, result dtypes, and unsupported cases.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant ExprMapNamespace
  participant EagerExprMapNamespace
  participant MapNamespace
  Caller->>ExprMapNamespace: call len()
  ExprMapNamespace->>EagerExprMapNamespace: append map.len expression node
  EagerExprMapNamespace->>MapNamespace: delegate len operation
Loading

Possibly related issues

Possibly related PRs

Suggested labels: enhancement, nested data

Suggested reviewers: danielgafni, fbruzzesi, marcogorelli

Merge Risk: Minimal · up to 9b980

This adds map.len() for Polars 2.0 or newer and marks it unsupported on other backends. No merge-blocking issue was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "feat: add map.len() for Polars" matches the main change. The PR adds a map.len() method to expressions and Series, backed by Polars 2.0 or newer. The diff also adds compliant, Arrow, DuckDB…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description states that the PR adds map.len() for expressions and Series backed by Polars 2.0 or newer, counts entries including null-valued ones, returns 0 for empty maps and null for null maps. …
Full details: Title check

Explanation

The title "feat: add map.len() for Polars" matches the main change. The PR adds a map.len() method to expressions and Series, backed by Polars 2.0 or newer. The diff also adds compliant, Arrow, DuckDB, Ibis, pandas-like, and Spark-like stubs that raise NotImplementedError. The title is specific and concise, and it follows the conventional commit style.

Full details: Description check

Explanation

The description states that the PR adds map.len() for expressions and Series backed by Polars 2.0 or newer, counts entries including null-valued ones, returns 0 for empty maps and null for null maps. It links related issue #4128, marks the Feature type, discloses AI assistance, and checks the Tests added and Documented the changes boxes. The template sections are all present and filled in, and the content matches the raw summary of changes.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b9e64244-ec8d-4900-9630-cd625eb37a6a
Commits

Reviewing files that changed from the base of the PR and between 752fcd4 and 9b980b6.

Files selected for processing: 13

  • docs/api-reference/expr_map.md
  • docs/api-reference/series_map.md
  • src/narwhals/_arrow/series_map.py
  • src/narwhals/_compliant/any_namespace.py
  • src/narwhals/_compliant/expr.py
  • src/narwhals/_duckdb/expr_map.py
  • src/narwhals/_ibis/expr_map.py
  • src/narwhals/_pandas_like/series_map.py
  • src/narwhals/_polars/utils.py
  • src/narwhals/_spark_like/expr_map.py
  • src/narwhals/expr_map.py
  • src/narwhals/series_map.py
  • tests/expr_and_series/map/len_test.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

This branch has not been deployed

No deployments
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.

1 participant