-
Notifications
You must be signed in to change notification settings - Fork 208
chore(typing): SparkLikeExpr properties
#2152
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
Changes from 17 commits
47e768d
7bb9131
89eeed5
f730d52
fda0fec
e19dcdf
a4b619c
2367125
fb48654
75e48cc
194ef83
ce3145d
ef015af
75e4477
4470a99
77a264c
fbf2459
fb2820b
311c7ea
7936aa8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from functools import partial | ||
| from typing import TYPE_CHECKING | ||
| from typing import overload | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pyspark.sql import Column | ||
|
|
@@ -107,47 +107,29 @@ def to_lowercase(self: Self) -> SparkLikeExpr: | |
| ) | ||
|
|
||
| def to_datetime(self: Self, format: str | None) -> SparkLikeExpr: # noqa: A002 | ||
| is_naive = ( | ||
| format is not None | ||
| and "%s" not in format | ||
| and "%z" not in format | ||
| and "Z" not in format | ||
| ) | ||
| function = ( | ||
| self._compliant_expr._F.to_timestamp_ntz | ||
| if is_naive | ||
| else self._compliant_expr._F.to_timestamp | ||
| ) | ||
| pyspark_format = strptime_to_pyspark_format(format) | ||
| format = ( | ||
| self._compliant_expr._F.lit(pyspark_format) if is_naive else pyspark_format | ||
| ) | ||
| F = self._compliant_expr._F # noqa: N806 | ||
| if not format: | ||
| function = F.to_timestamp | ||
| elif is_naive_format(format): | ||
| function = partial( | ||
| F.to_timestamp_ntz, format=F.lit(strptime_to_pyspark_format(format)) | ||
| ) | ||
| else: | ||
| format = strptime_to_pyspark_format(format) | ||
| function = partial(F.to_timestamp, format=format) | ||
| return self._compliant_expr._from_call( | ||
| lambda _input: function( | ||
| self._compliant_expr._F.replace( | ||
| _input, | ||
| self._compliant_expr._F.lit("T"), | ||
| self._compliant_expr._F.lit(" "), | ||
| ), | ||
| format=format, | ||
| ), | ||
| lambda _input: function(F.replace(_input, F.lit("T"), F.lit(" "))), | ||
| "to_datetime", | ||
| ) | ||
|
|
||
|
|
||
| @overload | ||
| def strptime_to_pyspark_format(format: None) -> None: ... | ||
|
|
||
|
|
||
| @overload | ||
| def strptime_to_pyspark_format(format: str) -> str: ... | ||
| def is_naive_format(format: str) -> bool: # noqa: A002 | ||
| return {"s", "z", "Z"}.isdisjoint(format) | ||
|
Member
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 don't really like how we're changing the logic here, the rest looks good though, well done! PR to address time zone aware parsing: #2166
Member
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. Thanks @MarcoGorelli I'm happy for that bit to be reverted and just keep the splitting out into the new function?
Member
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. This kind of
Member
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. sure! if you're happy with #2166 to add a test and adjust some logic, happy to then take this?
Member
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. Hopefully got it done (311c7ea) |
||
|
|
||
|
|
||
| def strptime_to_pyspark_format(format: str | None) -> str | None: # noqa: A002 | ||
| def strptime_to_pyspark_format(format: str) -> str: # noqa: A002 | ||
| """Converts a Python strptime datetime format string to a PySpark datetime format string.""" | ||
| # Mapping from Python strptime format to PySpark format | ||
| if format is None: | ||
| return None | ||
|
|
||
| # see https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html | ||
| # and https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.