Skip to content

Conversation

@liamholmes31
Copy link
Contributor

Description

Added cos methods for nw.Expr and nw.Series

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

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

Checklist

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



@pytest.mark.skipif(PANDAS_VERSION < (2, 1, 0), reason="nullable types require pandas2+")
def test_cos_dtype_pandas_pyarrow() -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

wasn't actually certain if this test was necessary given the above one?

narwhals/expr.py Outdated
|values: double |
|cos: double |
|---- |
|values: [[0,1.5707963267948966,3.141592653589793]]|
Copy link
Contributor Author

Choose a reason for hiding this comment

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

open to suggestions on making this output a bit less off-putting!

Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like:

result = df.with_columns(cos=nw.col("values").cos()).select(nw.all().round(4))

@FBruzzesi FBruzzesi added the enhancement New feature or request label Jan 6, 2026
@FBruzzesi FBruzzesi changed the title Feat/cos methods feat: Add {Expr,Series}.cos Jan 6, 2026
Copy link
Member

@FBruzzesi FBruzzesi left a comment

Choose a reason for hiding this comment

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

Thanks @liamholmes31 πŸ™πŸΌ

Happy to merge once CI get green(er) πŸ₯¦ (I think some of the values were copied-pasted from sine)

narwhals/expr.py Outdated
|values: double |
|cos: double |
|---- |
|values: [[0,1.5707963267948966,3.141592653589793]]|
Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like:

result = df.with_columns(cos=nw.col("values").cos()).select(nw.all().round(4))

@liamholmes31
Copy link
Contributor Author

Thanks @liamholmes31 πŸ™πŸΌ

Happy to merge once CI get green(er) πŸ₯¦ (I think some of the values were copied-pasted from sine)

oops forgot to check the doctests, thanks fixed and tidied the rounding here! 81e0448

Copy link
Member

@FBruzzesi FBruzzesi left a comment

Choose a reason for hiding this comment

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

Thanks @liamholmes31

@FBruzzesi FBruzzesi merged commit d7bb050 into narwhals-dev:main Jan 6, 2026
33 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enh]: add Expr.cos and Series.cos

3 participants