-
Notifications
You must be signed in to change notification settings - Fork 188
feat: Add {Expr,Series}.str.to_time
#3538
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
Open
camriddell
wants to merge
13
commits into
narwhals-dev:main
Choose a base branch
from
camriddell:feat/expr.str.to_time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
39ffb9e
feat: add {expr,Series}.str.to_time(…)
camriddell 783f4b3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8d4d5e2
ref: str.to_time HH:MM format infer to xfail for polars<1.30
camriddell 65176d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 917b462
add new PySpark TimeType & _spark_like str.to_time
camriddell 491826e
Revert "add new PySpark TimeType & _spark_like str.to_time"
camriddell f18a48b
Update narwhals/expr_str.py
camriddell 8575aaf
Update narwhals/series_str.py
camriddell 88feb2e
pandas_like...str.to_time auto-convert to pyarrow
camriddell b635afa
skip pandas<2.2 for str.to_time tests
camriddell 8af300c
ref: Time support logic for str.to_time tests
camriddell 7f11c64
ref: skip if pandas w/o pyarrow in str.to_time tests
camriddell 9de3233
str.to_time tests, ignore coverage from polars version xfails
camriddell 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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| - to_date | ||
| - to_datetime | ||
| - to_lowercase | ||
| - to_time | ||
| - to_titlecase | ||
| - to_uppercase | ||
| - zfill | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| - to_date | ||
| - to_datetime | ||
| - to_lowercase | ||
| - to_time | ||
| - to_titlecase | ||
| - to_uppercase | ||
| - zfill | ||
|
|
||
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
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,129 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from importlib.util import find_spec | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
|
|
||
| import narwhals as nw | ||
| from tests.utils import PANDAS_VERSION, POLARS_VERSION | ||
|
|
||
| if TYPE_CHECKING: | ||
| from tests.utils import Constructor, ConstructorEager | ||
|
|
||
| data = {"a": ["12:34:56"]} | ||
|
|
||
|
|
||
| def is_pandaslike_without_pyarrow(constructor: Constructor | ConstructorEager) -> bool: | ||
| """Returns True for pandas constructor that does not specify pyarrow and pyarrow is not importable. | ||
|
|
||
| Testing environments that do have pandas but not pyarrow available should to xfail to .str.to_time. | ||
|
|
||
| pandas does not natively support the Time datatype. As such, Narwhals | ||
| attempts to automatically convert pandas series to a pyarrow-backed pandas | ||
| series if pyarrow is available. | ||
| """ | ||
| name = constructor.__name__ | ||
| return ( | ||
| name.startswith(("pandas", "modin")) | ||
| and ("pyarrow" not in name) | ||
| and (find_spec("pyarrow") is None) | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.skipif(PANDAS_VERSION < (2, 2, 0), reason="pyarrow dtype not available") | ||
| def test_to_time(request: pytest.FixtureRequest, constructor: Constructor) -> None: | ||
| if is_pandaslike_without_pyarrow(constructor) or ( | ||
| "pyspark" in str(constructor) or "dask" in str(constructor) | ||
| ): | ||
| request.applymarker(pytest.mark.xfail) | ||
|
|
||
| expected = "12:34:56" | ||
|
|
||
| result = ( | ||
| nw.from_native(constructor(data)) | ||
| .lazy() | ||
| .select(b=nw.col("a").str.to_time(format="%H:%M:%S")) | ||
| .collect() | ||
| ) | ||
| assert isinstance(result.collect_schema()["b"], nw.Time) | ||
| assert str(result.item(row=0, column="b")) == expected | ||
|
|
||
|
|
||
| @pytest.mark.skipif(PANDAS_VERSION < (2, 2, 0), reason="pyarrow dtype not available") | ||
| def test_to_time_series( | ||
| request: pytest.FixtureRequest, constructor_eager: ConstructorEager | ||
| ) -> None: | ||
| if ( | ||
| is_pandaslike_without_pyarrow(constructor_eager) | ||
| or "pyspark" in str(constructor_eager) | ||
| or "dask" in str(constructor_eager) | ||
| ): | ||
| request.applymarker(pytest.mark.xfail) | ||
| expected = "12:34:56.000000000" if "cudf" in str(constructor_eager) else "12:34:56" | ||
| result = nw.from_native(constructor_eager(data), eager_only=True)["a"].str.to_time( | ||
| format="%H:%M:%S" | ||
| ) | ||
|
|
||
| assert isinstance(result.dtype, nw.Time) | ||
| assert str(result.item(0)) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("data", "expected"), | ||
| [({"a": ["12:34:56"]}, "12:34:56"), ({"a": ["12:34"]}, "12:34:00")], | ||
| ) | ||
| @pytest.mark.skipif(PANDAS_VERSION < (2, 2, 0), reason="pyarrow dtype not available") | ||
| def test_to_time_infer_fmt( | ||
| request: pytest.FixtureRequest, | ||
| constructor: Constructor, | ||
| data: dict[str, list[str]], | ||
| expected: str, | ||
| ) -> None: | ||
| if ( | ||
| ( | ||
| "polars" in str(constructor) | ||
| and POLARS_VERSION < (1, 30) | ||
| and data["a"][0].count(":") < 2 | ||
| ) | ||
| or is_pandaslike_without_pyarrow(constructor) | ||
| or "pyspark" in str(constructor) | ||
| or "dask" in str(constructor) | ||
| ): | ||
| request.applymarker(pytest.mark.xfail) | ||
|
|
||
| result = ( | ||
| nw.from_native(constructor(data)) | ||
| .lazy() | ||
| .select(b=nw.col("a").str.to_time()) | ||
| .collect() | ||
| ) | ||
| assert str(result.item(row=0, column="b")) == expected | ||
| assert isinstance(result.collect_schema()["b"], nw.Time) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("data", "expected"), | ||
| [({"a": ["12:34:56"]}, "12:34:56"), ({"a": ["12:34"]}, "12:34:00")], | ||
| ) | ||
| @pytest.mark.skipif(PANDAS_VERSION < (2, 2, 0), reason="pyarrow dtype not available") | ||
| def test_to_time_series_infer_fmt( | ||
| request: pytest.FixtureRequest, | ||
| constructor_eager: ConstructorEager, | ||
| data: dict[str, list[str]], | ||
| expected: str, | ||
| ) -> None: | ||
| if ( | ||
| ( | ||
| "polars" in str(constructor_eager) | ||
| and POLARS_VERSION < (1, 30) | ||
| and data["a"][0].count(":") < 2 | ||
| ) | ||
| or is_pandaslike_without_pyarrow(constructor_eager) | ||
| or "pyspark" in str(constructor_eager) | ||
| ): | ||
| request.applymarker(pytest.mark.xfail) | ||
|
|
||
| result = nw.from_native(constructor_eager(data), eager_only=True)["a"].str.to_time() | ||
| assert str(result.item(0)) == expected | ||
| assert isinstance(result.dtype, nw.Time) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.