Skip to content

fix: return null for modulo by zero on pandas-like backends (#4036) - #4062

Open
ioseba wants to merge 1 commit into
narwhals-dev:mainfrom
ioseba:fix-4036-mod-zero-pandas-nullable
Open

ioseba wants to merge 1 commit into
narwhals-dev:mainfrom
ioseba:fix-4036-mod-zero-pandas-nullable

Conversation

@ioseba

@ioseba ioseba commented Oct 11, 2026 •

Copy link
Copy Markdown

Fixes #4036.

Description

PR #4011 updated // 0 to return null on pandas-like backends via floordiv_null_on_zero. However, % was still using the raw operator, returning 0 for nullable integer dtypes (e.g. Int64, Int32, UInt32), whereas other backends return null.

This PR:

  • Introduces mod_null_on_zero in src/narwhals/_pandas_like/utils.py and routes __mod__ / __rmod__ through it in src/narwhals/_pandas_like/series.py.
  • Preserves native nullable integer dtypes while ensuring division/modulo by zero safely returns null (<NA>).
  • Adds dedicated unit tests in tests/expr_and_series/division_by_zero_test.py across nullable integer types (Int64, Int32, Int8, UInt32) for both scalar and column operands.

Summary by CodeRabbit

  • Bug Fixes
    • Modulo operations now return null results when the divisor is zero, including reflected operations and expressions.
    • Results preserve the input dtype in supported pandas-like backends.

@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 61faf724-18e2-428f-84ec-16dfa9895b62

📥 Commits

Reviewing files that changed from the base of the PR and between 4d6f7b0 and 0297c20.


📒 Files selected for processing (3)
  • src/narwhals/_pandas_like/series.py
  • src/narwhals/_pandas_like/utils.py
  • tests/expr_and_series/division_by_zero_test.py

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



📝 Walkthrough

Walkthrough

The pandas-like backend now applies null-on-zero handling to modulo operations. Tests cover scalar and column divisors, reflected operations, expressions, and dtype preservation.

Changes

Pandas-like modulo handling

Layer / File(s) Summary
Shared null-on-zero helpers
src/narwhals/_pandas_like/utils.py
A shared helper applies an operation while masking zero divisors. Floor division delegates to it, and a modulo helper uses it.
Series modulo integration and coverage
src/narwhals/_pandas_like/series.py, tests/expr_and_series/division_by_zero_test.py
Both Series modulo operand orders use the modulo helper. Tests cover Series and expression operations, zero and null divisors, and dtype preservation.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: fbruzzesi


Merge Risk: ⚪ Minimal · up to 0297c

No identified issue blocks merging; run the normal test checks before merge.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 11.76% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 17 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly and concisely identifies the main fix: returning null for modulo-by-zero on pandas-like backends.
Description check Passed The description explains the issue, the implementation, the affected dtypes, and the added test coverage. It includes a related issue reference but does not complete the template checkboxes for PR typ…
Linked Issues check Passed Issue #4036 requires null results for modulo by zero on pandas-like nullable integer data, for both operand orders and scalar or zero-valued column divisors. The PR adds _null_on_zero and `mod_null_…
Out of Scope Changes check Passed The changed source files implement the modulo-by-zero behavior from issue #4036. The added tests verify the requested scalar, column, reflected, mixed-divisor, null-scalar, and dtype cases. No unrelat…

  • Fix all pre-merge checks with AI
✨ 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

Comment @coderabbitai help to get the list of available commands.

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.

[Bug]: % 0 returns 0 on pandas nullable dtypes, where // 0 returns null after #4011

1 participant