Skip to content

Commit

Permalink
[UPDATE] float output to match short time delta
Browse files Browse the repository at this point in the history
Considering the variation of days between two dates, this function provides the user with a floating-point representation of the month difference, allowing for a more precise measurement of time intervals.
  • Loading branch information
akmalsoliev committed Jan 20, 2024
1 parent 7062510 commit 255aeee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/month_diff.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use chrono::Datelike;
use polars::prelude::*;
use pyo3_polars::derive::polars_expr;

Expand All @@ -14,14 +13,16 @@ pub fn month_diff(
}


let month_diff: Int32Chunked = start_dates.as_date_iter().zip(end_dates.as_date_iter()).map(
let month_diff: Float32Chunked = start_dates.as_date_iter().zip(end_dates.as_date_iter()).map(
|(s_arr, e_arr)| {
s_arr.zip(e_arr).map(
|(left, right)| {
{
let month_diff = right.month() as i32 - left.month() as i32;
let year_diff = (right.year() - left.year()) * 12;
month_diff + year_diff
let duration_delta = right.signed_duration_since(left);
let days_delta = duration_delta.num_days() as f32;
let year_fraction = days_delta / 365.25;
let month_diff = year_fraction * 12.;
month_diff
}
}
)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_month_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def test_month_diff():
.to_list()
)

assert [13, 2, 0, -12] == month_diff_list, (
expected_month_diff = [13.01026725769043, 2.0041067600250244, 0.0, -11.99178695678711]
assert expected_month_diff == month_diff_list, (
"The month difference list did not match the expected values.\n"
"Please check the function: 'month_diff.rs' for discrepancies."
)

0 comments on commit 255aeee

Please sign in to comment.