Skip to content

[Enh]: Add the negation unary operator for expressions and series - #3625

Merged
MarcoGorelli merged 1 commit into
narwhals-dev:mainfrom
pedro-villanueva-bcom:add_unary_negation
May 15, 2026
Merged

MarcoGorelli merged 1 commit into
narwhals-dev:mainfrom
pedro-villanueva-bcom:add_unary_negation

Conversation

@pedro-villanueva-bcom

Copy link
Copy Markdown
Contributor

Description

Expressions and Series allow for the usual arithmetic except for the unary negation:

>>> import narwhals as nw
>>> import pandas as pd
>>> -nw.lit(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary -: 'Expr'
>>> -nw.col("col1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary -: 'Expr'
>>> -nw.from_native(pd.Series([1, 2, 3, 4]), series_only=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary -: 'Series'

This PR allows this:

>>> import narwhals as nw
>>> import pandas as pd
>>> -nw.lit(5)
lit(value=5, dtype=None).__neg__()
>>> -nw.col("col1")
col(col1).__neg__()
>>> -nw.from_native(pd.Series([1, 2, 3, 4]), series_only=True)
┌───────────────┐
|Narwhals Series|
|---------------|
| 0   -1        |
| 1   -2        |
| 2   -3        |
| 3   -4        |
| dtype: int64  |
└───────────────┘

And of course

>>> df = nw.from_native(pd.DataFrame({"a": [1, 2, 3, 4], "b": [0.2, 0.5, None, 5.5]}))
>>> df.select( -nw.col("a"), -nw.col("b"))
┌──────────────────┐
|Narwhals DataFrame|
|------------------|
|       a    b     |
|    0 -1 -0.2     |
|    1 -2 -0.5     |
|    2 -3  NaN     |
|    3 -4 -5.5     |
└──────────────────┘

I basically copied the invert functionality.

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

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

Related issues

  • Related issue #<issue number>
  • Closes #<issue number>

Checklist

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

@MarcoGorelli MarcoGorelli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoGorelli
MarcoGorelli merged commit 4ea3ede into narwhals-dev:main May 15, 2026
40 checks passed
dangotbanned added a commit that referenced this pull request May 19, 2026
commit 9d015e2031acd5e10404da72a7d698c632446c49
Author: Marco Edward Gorelli <[email protected]>
Date:   Sat May 16 09:51:39 2026 +0100

    release: Bump version to 2.21.2 (#3630)

commit 8674e44f657110b45aedffb8c1343d5b0505bb01
Author: Marco Edward Gorelli <[email protected]>
Date:   Sat May 16 09:37:56 2026 +0100

    release: Bump version to 2.21.1 (#3628)

commit 739dc579adfd38a75764d6d2c09ed42b0f2e887a
Author: Marco Edward Gorelli <[email protected]>
Date:   Sat May 16 09:35:33 2026 +0100

    ci: remove `downstream_tests_slow` (#3629)

commit 1f36279bb07203628ddd18a691cf98d4c78780d3
Author: Marco Edward Gorelli <[email protected]>
Date:   Sat May 16 09:17:10 2026 +0100

    Revert "fix: Allow `float('nan')` as value in join for duckdb (#3555)" (#3627)

    This reverts commit 0d7f352.

commit 1e50d020e3ca5e5c5dfe3486e53190e87c60b66c
Author: Pedro <[email protected]>
Date:   Fri May 15 13:05:01 2026 +0200

    [Enh]: Add the negation unary operator for expressions and series (#3625)

    negation unary operator for expressions and series

commit 37ea7953f6b455736bf6ae353ca257f468b3b083
Author: Francesco Bruzzesi <[email protected]>
Date:   Wed May 13 10:38:42 2026 +0200

    ci: Unpin "temporary" CI pins (#3618)

    * ci: Unpin 'temporary' CI pins

    * rollback formulaic and pointblank

commit 4ff3a1f545924e5e38e76c4d8a71de2010a2f4d6
Author: Francesco Bruzzesi <[email protected]>
Date:   Tue May 12 19:11:11 2026 +0200

    chore: Prepare for future pandas inplace deprecation (#3616)
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.

2 participants