-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Add time_zone
to Datetime
result
s
#33
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
dfrankland
wants to merge
9
commits into
Quantco:main
Choose a base branch
from
dfrankland:add-time_zone-to-datetime-results
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b61a268
add `time_zone` to `Datetime` `result`s
dfrankland 58e510d
add sql schema tests for datetime with time zone
dfrankland 0796225
add better documentation of `time_zone` argument
dfrankland 830fff8
use polars default for time_unit
dfrankland fa62831
switch to using dtype validation for datetime timezones
dfrankland 30e3538
backout oracle updates
dfrankland 70f2973
remove unneeded code
dfrankland 2ee9d50
update test name
dfrankland bf6ac85
remove use of ZoneInfo
dfrankland 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 | ||||
---|---|---|---|---|---|---|
|
@@ -9,7 +9,12 @@ | |||||
|
||||||
import polars as pl | ||||||
|
||||||
from dataframely._compat import pa, sa, sa_mssql, sa_TypeEngine | ||||||
from dataframely._compat import ( | ||||||
pa, | ||||||
sa, | ||||||
sa_mssql, | ||||||
sa_TypeEngine, | ||||||
) | ||||||
from dataframely._polars import ( | ||||||
EPOCH_DATETIME, | ||||||
date_matches_resolution, | ||||||
|
@@ -265,6 +270,7 @@ def __init__( | |||||
max: dt.datetime | None = None, | ||||||
max_exclusive: dt.datetime | None = None, | ||||||
resolution: str | None = None, | ||||||
time_zone: str | dt.tzinfo | None = None, | ||||||
check: Callable[[pl.Expr], pl.Expr] | None = None, | ||||||
alias: str | None = None, | ||||||
metadata: dict[str, Any] | None = None, | ||||||
|
@@ -284,6 +290,9 @@ def __init__( | |||||
the formatting language used by :mod:`polars` datetime ``round`` method. | ||||||
For example, a value ``1h`` expects all datetimes to be full hours. Note | ||||||
that this setting does *not* affect the storage resolution. | ||||||
time_zone: The time zone that datetimes in the column must have. The time | ||||||
zone must use a valid IANA time zone name identifier e.x. ``Etc/UTC`` or | ||||||
``America/New_York``. | ||||||
check: A custom check to run for this column. Must return a non-aggregated | ||||||
boolean expression. | ||||||
alias: An overwrite for this column's name which allows for using a column | ||||||
|
@@ -317,10 +326,11 @@ def __init__( | |||||
metadata=metadata, | ||||||
) | ||||||
self.resolution = resolution | ||||||
self.time_zone = time_zone | ||||||
|
||||||
@property | ||||||
def dtype(self) -> pl.DataType: | ||||||
return pl.Datetime() | ||||||
return pl.Datetime(time_zone=self.time_zone) | ||||||
|
||||||
def validation_rules(self, expr: pl.Expr) -> dict[str, pl.Expr]: | ||||||
result = super().validation_rules(expr) | ||||||
|
@@ -329,16 +339,22 @@ def validation_rules(self, expr: pl.Expr) -> dict[str, pl.Expr]: | |||||
return result | ||||||
|
||||||
def sqlalchemy_dtype(self, dialect: sa.Dialect) -> sa_TypeEngine: | ||||||
timezone_enabled = self.time_zone is not None | ||||||
match dialect.name: | ||||||
case "mssql": | ||||||
# sa.DateTime wrongly maps to DATETIME | ||||||
return sa_mssql.DATETIME2(6) | ||||||
return sa_mssql.DATETIME2(6, timezone=timezone_enabled) | ||||||
case _: | ||||||
return sa.DateTime() | ||||||
return sa.DateTime(timezone=timezone_enabled) | ||||||
|
||||||
@property | ||||||
def pyarrow_dtype(self) -> pa.DataType: | ||||||
return pa.timestamp("us") | ||||||
time_zone = ( | ||||||
self.time_zone.tzname(None) | ||||||
if isinstance(self.time_zone, dt.timezone) | ||||||
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. Doesn't this mirror the type annotation? 😄
Suggested change
|
||||||
else self.time_zone | ||||||
) | ||||||
return pa.timestamp("us", time_zone) | ||||||
|
||||||
def _sample_unchecked(self, generator: Generator, n: int) -> pl.Series: | ||||||
return generator.sample_datetime( | ||||||
|
@@ -354,6 +370,7 @@ def _sample_unchecked(self, generator: Generator, n: int) -> pl.Series: | |||||
allow_null_response=True, | ||||||
), | ||||||
resolution=self.resolution, | ||||||
time_zone=self.time_zone, | ||||||
null_probability=self._null_probability, | ||||||
) | ||||||
|
||||||
|
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.
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.
Nothing changed here, right?