Skip to content

Commit d7bb050

Browse files
feat: Add {Expr,Series}.cos (#3392)
--------- Co-authored-by: Francesco Bruzzesi <[email protected]>
1 parent 00858bb commit d7bb050

File tree

13 files changed

+163
-1
lines changed

13 files changed

+163
-1
lines changed

docs/api-reference/expr.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- cast
1313
- ceil
1414
- clip
15+
- cos
1516
- count
1617
- cum_count
1718
- cum_max

docs/api-reference/series.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- cast
1919
- ceil
2020
- clip
21+
- cos
2122
- count
2223
- cum_count
2324
- cum_max

narwhals/_arrow/series.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,9 @@ def sqrt(self) -> Self:
11071107
def sin(self) -> Self:
11081108
return self._with_native(pc.sin(self.native))
11091109

1110+
def cos(self) -> Self:
1111+
return self._with_native(pc.cos(self.native))
1112+
11101113
def any_value(
11111114
self, *, ignore_nulls: bool, _return_py_scalar: bool = True
11121115
) -> PythonLiteral:

narwhals/_compliant/column.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def round(self, decimals: int) -> Self: ...
135135
def floor(self) -> Self: ...
136136
def ceil(self) -> Self: ...
137137
def shift(self, n: int) -> Self: ...
138+
def cos(self) -> Self: ...
138139
def sin(self) -> Self: ...
139140
def unique(self) -> Self: ...
140141

narwhals/_compliant/expr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,9 @@ def exp(self) -> Self:
812812
def sin(self) -> Self:
813813
return self._reuse_series("sin")
814814

815+
def cos(self) -> Self:
816+
return self._reuse_series("cos")
817+
815818
def sqrt(self) -> Self:
816819
return self._reuse_series("sqrt")
817820

narwhals/_dask/expr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,11 @@ def sin(self) -> Self:
642642

643643
return self._with_callable(da.sin)
644644

645+
def cos(self) -> Self:
646+
import dask.array as da
647+
648+
return self._with_callable(da.cos)
649+
645650
def sqrt(self) -> Self:
646651
import dask.array as da
647652

narwhals/_pandas_like/series.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,21 @@ def sin(self) -> Self:
11161116

11171117
return self._with_native(result_native)
11181118

1119+
def cos(self) -> Self:
1120+
native = self.native
1121+
if self.is_native_dtype_pyarrow(native.dtype):
1122+
import pyarrow.compute as pc
1123+
1124+
result_native = self._apply_pyarrow_compute_func(
1125+
native,
1126+
pc.cos, # type: ignore[arg-type]
1127+
)
1128+
else:
1129+
array_func = self._array_funcs.cos
1130+
result_native = self._apply_array_func(native, array_func)
1131+
1132+
return self._with_native(result_native)
1133+
11191134
def is_native_dtype_pyarrow(self, native_dtype: Any) -> bool:
11201135
impl = self._implementation
11211136
return get_dtype_backend(native_dtype, implementation=impl) == "pyarrow"

narwhals/_polars/expr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ def struct(self) -> PolarsExprStructNamespace:
336336
arg_true: Method[Self]
337337
ceil: Method[Self]
338338
count: Method[Self]
339+
cos: Method[Self]
339340
cum_max: Method[Self]
340341
cum_min: Method[Self]
341342
cum_prod: Method[Self]

narwhals/_polars/series.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"arg_true",
8484
"ceil",
8585
"clip",
86+
"cos",
8687
"count",
8788
"cum_max",
8889
"cum_min",
@@ -710,6 +711,7 @@ def struct(self) -> PolarsSeriesStructNamespace:
710711
arg_true: Method[Self]
711712
ceil: Method[Self]
712713
count: Method[int]
714+
cos: Method[Self]
713715
cum_max: Method[Self]
714716
cum_min: Method[Self]
715717
cum_prod: Method[Self]

narwhals/_sql/expr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,9 @@ def ceil(self) -> Self:
584584
def sin(self) -> Self:
585585
return self._with_elementwise(lambda expr: self._function("sin", expr))
586586

587+
def cos(self) -> Self:
588+
return self._with_elementwise(lambda expr: self._function("cos", expr))
589+
587590
def sqrt(self) -> Self:
588591
def _sqrt(expr: NativeExprT) -> NativeExprT:
589592
return self._when(

0 commit comments

Comments
 (0)