-
Notifications
You must be signed in to change notification settings - Fork 167
feat: Add support for {Expr,Series}.str.to_titlecase
#3116
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 all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
234f04e
WIP
FBruzzesi 57590a4
xfail sql-like
FBruzzesi b4d0d36
additional test and docstrings
FBruzzesi 83fd5eb
got creative with polars
FBruzzesi cd25427
duckdb implementation
FBruzzesi ddd6d41
not implemented for old duckdb
FBruzzesi 93c413b
merge main
FBruzzesi 1ef1ad5
pragma no cover old version
FBruzzesi 4164bb7
test: Add a `polars` edge case
dangotbanned 599fb25
fix leading non-alphanum
FBruzzesi aa40799
pyspark :)
FBruzzesi 26d048b
Merge branch 'main' into feat/str-to-titlecase
FBruzzesi 0bba2d9
use duckdb lambda expression
FBruzzesi a9963e1
add no cover for entire method since pyspark runs its CI
FBruzzesi 1a4d17a
docstrings update, skip old duckdb
FBruzzesi 02edde4
refactor(suggestion): Lightly sugar `LambdaExpression`
dangotbanned d93e932
refactor: Rename to `elem`
dangotbanned 7bba817
merge main
FBruzzesi 99952d2
use @requires.backend_version for duckdb
FBruzzesi 19fdcda
enable sqlframe
FBruzzesi 88bb25b
Merge branch 'main' into feat/str-to-titlecase
FBruzzesi 95ffe8e
apply (some) suggestions
FBruzzesi e55e269
Merge branch 'main' into feat/str-to-titlecase
FBruzzesi 6128b49
refactor test, change table to list in docstring warning
FBruzzesi 515e4a1
Merge branch 'main' into feat/str-to-titlecase
dangotbanned 9a8974d
ci: Exclude coverage for `BACKEND_VERSION <`
dangotbanned 5728b9b
Apply suggestions
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
Some comments aren't visible on the classic Files Changed page.
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
- to_date | ||
- to_datetime | ||
- to_lowercase | ||
- to_titlecase | ||
- to_uppercase | ||
- zfill | ||
show_source: false | ||
|
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 @@ | |
- to_date | ||
- to_datetime | ||
- to_lowercase | ||
- to_titlecase | ||
- to_uppercase | ||
- zfill | ||
show_source: false | ||
|
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 |
---|---|---|
|
@@ -5,9 +5,11 @@ | |
|
||
from narwhals._spark_like.utils import strptime_to_pyspark_format | ||
from narwhals._sql.expr_str import SQLExprStringNamespace | ||
from narwhals._utils import _is_naive_format, not_implemented | ||
from narwhals._utils import _is_naive_format, not_implemented, requires | ||
|
||
if TYPE_CHECKING: | ||
from sqlframe.base.column import Column | ||
|
||
from narwhals._spark_like.expr import SparkLikeExpr | ||
|
||
|
||
|
@@ -33,4 +35,30 @@ def to_date(self, format: str | None) -> SparkLikeExpr: | |
lambda expr: F.to_date(expr, format=strptime_to_pyspark_format(format)) | ||
) | ||
|
||
def to_titlecase(self) -> SparkLikeExpr: | ||
impl = self.compliant._implementation | ||
sqlframe_required_version = (3, 43, 1) | ||
if ( | ||
impl.is_sqlframe() | ||
and (version := impl._backend_version()) < sqlframe_required_version | ||
): # pragma: no cover | ||
required_str = requires._unparse_version(sqlframe_required_version) | ||
found_str = requires._unparse_version(version) | ||
msg = ( | ||
f"`str.to_titlecase` is only available in 'sqlframe>={required_str}', " | ||
f"found version {found_str!r}." | ||
) | ||
raise NotImplementedError(msg) | ||
Comment on lines
+38
to
+51
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. I guess this would be a case that #3059 would be nice for? This'll do for now though π |
||
|
||
def _to_titlecase(expr: Column) -> Column: | ||
F = self.compliant._F | ||
lower_expr = F.lower(expr) | ||
extract_expr = F.regexp_extract_all( | ||
lower_expr, regexp=F.lit(r"[a-z0-9]*[^a-z0-9]*"), idx=0 | ||
) | ||
capitalized_expr = F.transform(extract_expr, f=F.initcap) | ||
return F.array_join(capitalized_expr, delimiter="") | ||
|
||
return self.compliant._with_elementwise(_to_titlecase) | ||
|
||
replace = not_implemented() |
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
Oops, something went wrong.
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.