-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
chore(python): Switch over some of the custom Python date/time conversions to native PyO3 conversions #16203
Merged
ritchie46
merged 9 commits into
pola-rs:main
from
itamarst:16158-native-pyo3-datetime-support
May 14, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
39c1b3f
Start converting to native PyO3 chrono support.
pythonspeed 2451201
Convert more functions to PyO3 conversion instead of custom Polars co…
pythonspeed 5aea1ea
Another replacement of Python conversion with PyO3 conversion.
pythonspeed 2c69743
Start switching away from to-Python serialization.
pythonspeed b9cc590
Pacify clippy.
pythonspeed 84a2141
Merge remote-tracking branch 'origin/main' into 16158-native-pyo3-dat…
pythonspeed 30fdd5b
Ended up not using chrono-tz in this iteration.
pythonspeed 07b5252
cargo fmt
pythonspeed e65955a
Merge branch 'main' into 16158-native-pyo3-datetime-support
itamarst 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,31 @@ | ||
//! Utilities for converting dates, times, datetimes, and so on. | ||
|
||
use polars::datatypes::TimeUnit; | ||
use polars_core::export::chrono::{NaiveDateTime, NaiveTime, TimeDelta}; | ||
|
||
pub fn elapsed_offset_to_timedelta(elapsed: i64, time_unit: TimeUnit) -> TimeDelta { | ||
let (in_second, nano_multiplier) = match time_unit { | ||
TimeUnit::Nanoseconds => (1_000_000_000, 1), | ||
TimeUnit::Microseconds => (1_000_000, 1_000), | ||
TimeUnit::Milliseconds => (1_000, 1_000_000), | ||
}; | ||
let mut elapsed_sec = elapsed / in_second; | ||
let mut elapsed_nanos = nano_multiplier * (elapsed % in_second); | ||
if elapsed_nanos < 0 { | ||
// TimeDelta expects nanos to always be positive. | ||
elapsed_sec -= 1; | ||
elapsed_nanos += 1_000_000_000; | ||
} | ||
TimeDelta::new(elapsed_sec, elapsed_nanos as u32).unwrap() | ||
} | ||
|
||
/// Convert time-units-since-epoch to a more structured object. | ||
pub fn timestamp_to_naive_datetime(since_epoch: i64, time_unit: TimeUnit) -> NaiveDateTime { | ||
NaiveDateTime::UNIX_EPOCH + elapsed_offset_to_timedelta(since_epoch, time_unit) | ||
} | ||
|
||
/// Convert nanoseconds-since-midnight to a more structured object. | ||
pub fn nanos_since_midnight_to_naivetime(nanos_since_midnight: i64) -> NaiveTime { | ||
NaiveTime::from_hms_opt(0, 0, 0).unwrap() | ||
+ elapsed_offset_to_timedelta(nanos_since_midnight, TimeUnit::Nanoseconds) | ||
} |
This file contains 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 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
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.
Tried nanoseconds here, it broke the tests, so stuck to micro/milliseconds only.