-
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 6 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,113 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
|
|
||
| import narwhals as nw | ||
| from tests.utils import POLARS_VERSION | ||
|
|
||
| if TYPE_CHECKING: | ||
| from tests.utils import Constructor, ConstructorEager | ||
|
|
||
| data = {"a": ["12:34:56"]} | ||
|
|
||
|
|
||
| def test_to_time(request: pytest.FixtureRequest, constructor: Constructor) -> None: | ||
| if ( | ||
| ("pandas" in str(constructor) and "pyarrow" not in str(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")) | ||
| ) | ||
| result_schema = result.collect_schema() | ||
| assert isinstance(result_schema["b"], nw.Time) | ||
| result_item = result.collect().item(row=0, column="b") | ||
| assert str(result_item) == expected | ||
|
|
||
|
|
||
| def test_to_time_series( | ||
| request: pytest.FixtureRequest, constructor_eager: ConstructorEager | ||
| ) -> None: | ||
| if ( | ||
| ("pandas" in str(constructor_eager) and "pyarrow" not in str(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" | ||
| ) | ||
| ).item(0) | ||
| assert str(result) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("data", "expected"), | ||
| [({"a": ["12:34:56"]}, "12:34:56"), ({"a": ["12:34"]}, "12:34:00")], | ||
| ) | ||
| 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 ("pandas" in str(constructor) and "pyarrow" not in str(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() | ||
| .item(row=0, column="b") | ||
| ) | ||
| assert str(result) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("data", "expected"), | ||
| [({"a": ["12:34:56"]}, "12:34:56"), ({"a": ["12:34"]}, "12:34:00")], | ||
| ) | ||
| 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 ( | ||
| "pandas" in str(constructor_eager) and "pyarrow" not in str(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() | ||
| ).item(0) | ||
| assert str(result) == expected |
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.
Should we follow the same "pattern" that we use when converting from any pandas-like dtype to one that is supported only with pyarrow instead of directly requiring that the dtype is already pyarrow-backed?
I refer to:
narwhals/narwhals/_pandas_like/utils.py
Lines 549 to 563 in 13e6024
Uh oh!
There was an error while loading. Please reload this page.
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.
I was following the pattern in _pandas_like...str.spit.
Shall I change both this instance and the one in my PR to follow the reference in
_pandas_like.utils.narwhals_to_native_arrow_dtype?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.
Thanks for pointing that out! I guess it's some inconsistency!
I would be ok casting in behalf of the user in these cases, but happy to hear what others think about this
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.
Updated, I let the mechanics of
.cast(Time)handle the auto-conversion after stepping through the datetime dtype.I also added an xfail for testing environments that are running the non-pyarrow pandas-like constructors but do not have pyarrow installed/available.