-
Notifications
You must be signed in to change notification settings - Fork 177
feat: Add {Expr,Series}.cos
#3392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7933439
feat: added sin method for expr and series
liamholmes31 34710ae
removed sin from CompliantExpr
liamholmes31 e7c0669
Merge branch 'main' into feat/sin_methods
liamholmes31 77f5ebd
restored any_value methods dropped in conflict resolutions
liamholmes31 b96b1a9
fix: Add `ComplianColumn.sin`
dangotbanned ffb77ba
setup cos expr/series method and started adding tests
liamholmes31 8840be4
fixed float typo in sin_test.py
liamholmes31 70d22ea
fix: fixed float type in cos_test.py
liamholmes31 6789e92
Merge remote-tracking branch 'upstream/main' into feat/cos_methods
liamholmes31 06891c1
feat: added nw.Series/Expr cos method
liamholmes31 81e0448
fix: rounding and typos with nw.Expr/Series cos doctests
liamholmes31 a5e627b
typo "Float32" -> "float32"
FBruzzesi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| - cast | ||
| - ceil | ||
| - clip | ||
| - cos | ||
| - count | ||
| - cum_count | ||
| - cum_max | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| - cast | ||
| - ceil | ||
| - clip | ||
| - cos | ||
| - count | ||
| - cum_count | ||
| - cum_max | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from math import pi | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
|
|
||
| import narwhals as nw | ||
| from tests.utils import ( | ||
| PANDAS_VERSION, | ||
| PYARROW_VERSION, | ||
| Constructor, | ||
| ConstructorEager, | ||
| assert_equal_data, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from narwhals.typing import DTypeBackend | ||
|
|
||
| data = {"a": [-pi, -pi / 2, 0.0, pi / 2, pi]} | ||
|
|
||
| expected = [-1, 0, 1, 0, -1] | ||
|
|
||
|
|
||
| @pytest.mark.filterwarnings("ignore::RuntimeWarning") | ||
| def test_cos_expr(constructor: Constructor) -> None: | ||
| df = nw.from_native(constructor(data)) | ||
| result = df.select(nw.col("a").cos()) | ||
| assert_equal_data(result, {"a": expected}) | ||
|
|
||
|
|
||
| @pytest.mark.filterwarnings("ignore::RuntimeWarning") | ||
| def test_cos_series(constructor_eager: ConstructorEager) -> None: | ||
| series = nw.from_native(constructor_eager(data), eager_only=True)["a"] | ||
| result = series.cos() | ||
| assert_equal_data({"a": result}, {"a": expected}) | ||
|
|
||
|
|
||
| PYARROW_UNAVAILABLE = PYARROW_VERSION == (0, 0, 0) | ||
| reason = "nullable types require pandas2+" | ||
| require_pd_2_1 = pytest.mark.skipif( | ||
| PANDAS_VERSION < (2, 1, 0) or PYARROW_UNAVAILABLE, reason=reason | ||
| ) | ||
| require_pd_2_0 = pytest.mark.skipif(PANDAS_VERSION < (2, 0, 0), reason=reason) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "dtype_backend", | ||
| [ | ||
| pytest.param("pyarrow", marks=require_pd_2_1), | ||
| pytest.param("numpy_nullable", marks=require_pd_2_0), | ||
| None, | ||
| ], | ||
| ) | ||
| def test_cos_dtype_pandas(dtype_backend: DTypeBackend) -> None: | ||
| pytest.importorskip("pandas") | ||
| import pandas as pd | ||
|
|
||
| s = pd.Series([-pi / 2, None, pi / 2], name="a", dtype="Float32", index=[8, 7, 6]) | ||
FBruzzesi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if dtype_backend: | ||
| s = s.convert_dtypes(dtype_backend=dtype_backend) | ||
| result = nw.from_native(s, series_only=True).cos().to_native() | ||
| expected = pd.Series([0, None, 0], name="a", dtype=s.dtype, index=[8, 7, 6]) | ||
| pd.testing.assert_series_equal(result, expected, rtol=0, atol=1e-6) | ||
|
|
||
|
|
||
| @pytest.mark.skipif(PANDAS_VERSION < (2, 1, 0), reason="nullable types require pandas2+") | ||
| def test_cos_dtype_pandas_pyarrow() -> None: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| pytest.importorskip("pandas") | ||
| pytest.importorskip("pyarrow") | ||
| import pandas as pd | ||
|
|
||
| s = pd.Series( | ||
| [-pi / 2, None, pi / 2], name="a", dtype="Float32[pyarrow]", index=[8, 7, 6] | ||
| ) | ||
| result = nw.from_native(s, series_only=True).cos().to_native() | ||
| expected = pd.Series( | ||
| [0, None, 0], name="a", dtype="Float32[pyarrow]", index=[8, 7, 6] | ||
| ) | ||
| pd.testing.assert_series_equal(result, expected, rtol=0, atol=1e-6) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like: