-
-
Notifications
You must be signed in to change notification settings - Fork 320
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
Enhancement: Add support for timezone-flexible DateTime (#1352) #1902
base: main
Are you sure you want to change the base?
Conversation
529178c
to
cd998c8
Compare
09dfd96
to
ca6046a
Compare
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 the contribution @max-raphael !
can we rename this to time_zone_agnostic
to match the polars dtype engine implementation? #1589
I believe it's the same behavior
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1902 +/- ##
==========================================
- Coverage 94.28% 93.28% -1.00%
==========================================
Files 91 121 +30
Lines 7013 9338 +2325
==========================================
+ Hits 6612 8711 +2099
- Misses 401 627 +226 ☔ View full report in Codecov by Sentry. |
…1352) Signed-off-by: Max Raphael <[email protected]> Enhancement: Add support for timezone-flexible DateTime (unionai-oss#1352) Signed-off-by: Max Raphael <[email protected]>
ca6046a
to
cfbe866
Compare
No problem @cosmicBboy! I've just pushed a change that uses |
Just checking back in here @cosmicBboy, is there anything else I need to do in order for the rest of the workflow checks to run? |
Enhancement: Add support for timezone-flexible DateTime (#1352)
This pull request introduces a new feature to the DateTime class, allowing for more flexible handling of timezones during datetime coercion and validation. Prevously, if a field was type DateTime and it was going to receive tz-aware data:
This feature enables users to define a DateTime field and not define the timezone up front, by setting
timezone_flexible=True
. However, ifcoerce=True
, a timezone (tz
) must also be defined. This ensures consistent handling and avoids ambiguity when coercing datetime values.Example of pandera DataFrameModel definition
Functionality Specifics:
If coerce is
False
:tz
, as specifying it up front without coercion defeats the purpose of using this. If you want to validate that the data is of a timezone that is known at the time of class definition, do not settimezone_flexible=True
.Examples:
Example 1
Input Data:
raises an exception.
Example 2
Input Data:
results in output being the same as input
Note
Setting
DateTime(tz="America/Chicago")
has no effect on the timezone becausecoerce=False
. That functionality is already handled bytimezone_flexible=False
.If coerce is
True
:Examples:
Example 1
Input data:
Setting DateTime(tz="UTC") and coerce=True results in
Example 2
Input data:
Setting DateTime(tz="America/Chicago") and coerce=True results in
Implementation details
Because this feature is intended to be used when the timezone may not known at the time of class definition (or there are multiple timezones),
self.type
andself.tz
must be defined during validation, and possibly coercion (if coerce is True). Therefore, bothDateTime.coerce()
andDateTime.check()
now contain conditional logic to infer and define the type and tz of the field based on the data its received.