-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from MarcoGorelli/sub
Add Expr.sub
- Loading branch information
Showing
14 changed files
with
308 additions
and
37 deletions.
There are no files selected for viewing
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
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Marco Edward Gorelli | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
|
@@ -4,15 +4,16 @@ build-backend = "maturin" | |
|
||
[project] | ||
name = "polars-business" | ||
requires-python = ">=3.8" | ||
description = "Business day utilities for Polars" | ||
readme = "README.md" | ||
authors = [ | ||
{ name="Marco Gorelli", email="[email protected]" }, | ||
] | ||
license = { file = "LICENSE" } | ||
classifiers = [ | ||
"Programming Language :: Rust", | ||
"Programming Language :: Python :: Implementation :: CPython", | ||
"Programming Language :: Python :: Implementation :: PyPy", | ||
] | ||
version = "0.1.29" | ||
authors = [ | ||
{ name="Marco Gorelli", email="[email protected]" }, | ||
] | ||
description = "Business day utilities for Polars" | ||
readme = "README.md" | ||
requires-python = ">=3.8" |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod business_days; | ||
mod expressions; | ||
mod sub; | ||
|
||
#[cfg(target_os = "linux")] | ||
use jemallocator::Jemalloc; | ||
|
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,79 @@ | ||
use crate::business_days::weekday; | ||
use polars::prelude::arity::binary_elementwise; | ||
use polars::prelude::*; | ||
|
||
fn date_diff(mut start_date: i32, mut end_date: i32) -> i32 { | ||
let swapped = start_date > end_date; | ||
if swapped { | ||
(start_date, end_date) = (end_date, start_date); | ||
start_date += 1; | ||
end_date += 1; | ||
} | ||
|
||
let mut start_weekday = weekday(start_date); | ||
let end_weekday = weekday(end_date); | ||
|
||
if start_weekday == 6 { | ||
start_date += 2; | ||
start_weekday = 1; | ||
} else if start_weekday == 7 { | ||
start_date += 1; | ||
start_weekday = 1; | ||
} | ||
if end_weekday == 6 { | ||
end_date += 2; | ||
} else if end_weekday == 7 { | ||
end_date += 1; | ||
} | ||
|
||
let diff = end_date - start_date; | ||
|
||
let whole_weeks = diff / 7; | ||
let mut count = 0; | ||
count += whole_weeks * 5; | ||
start_date += whole_weeks * 7; | ||
while start_date < end_date { | ||
if start_weekday < 6 { | ||
count += 1; | ||
} | ||
start_date += 1; | ||
start_weekday += 1; | ||
if start_weekday > 7 { | ||
start_weekday = 1; | ||
} | ||
} | ||
if swapped { | ||
-count | ||
} else { | ||
count | ||
} | ||
} | ||
|
||
pub(crate) fn impl_sub( | ||
end_dates: &Series, | ||
start_dates: &Series, | ||
// holidays: Vec<i32>, | ||
// weekend: Vec<i32>, | ||
) -> PolarsResult<Series> { | ||
if (start_dates.dtype() != &DataType::Date) || (end_dates.dtype() != &DataType::Date) { | ||
polars_bail!(InvalidOperation: "polars_business sub only works on Date type. Please cast to Date first."); | ||
} | ||
let start_dates = start_dates.date()?; | ||
let end_dates = end_dates.date()?; | ||
let out = match end_dates.len() { | ||
1 => { | ||
if let Some(end_date) = end_dates.get(0) { | ||
start_dates.apply(|x_date| x_date.map(|start_date| date_diff(start_date, end_date))) | ||
} else { | ||
Int32Chunked::full_null(start_dates.name(), start_dates.len()) | ||
} | ||
} | ||
_ => binary_elementwise(start_dates, end_dates, |opt_s, opt_n| { | ||
match (opt_s, opt_n) { | ||
(Some(start_date), Some(end_date)) => Some(date_diff(start_date, end_date)), | ||
_ => None, | ||
} | ||
}), | ||
}; | ||
Ok(out.into_series()) | ||
} |
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,51 @@ | ||
# type: ignore | ||
import timeit | ||
import warnings | ||
import numpy as np | ||
|
||
BENCHMARKS = [1, 2, 3, 4] | ||
|
||
SIZE = 1_000_000 | ||
|
||
# BENCHMARK 1: NO HOLIDAYS INVOLVED | ||
|
||
setup = f""" | ||
import polars as pl | ||
import polars_business as plb | ||
from datetime import date | ||
import numpy as np | ||
import pandas as pd | ||
import holidays | ||
import warnings | ||
dates = pl.date_range(date(2020, 1, 1), date(2024, 1, 1), closed='left', eager=True) | ||
size = {SIZE} | ||
start_dates = np.random.choice(dates, size) | ||
end_dates = np.random.choice(dates, size) | ||
df = pl.DataFrame({{ | ||
'start_date': start_dates, | ||
'end_date': end_dates, | ||
}}) | ||
""" | ||
|
||
|
||
def time_it(statement): | ||
results = ( | ||
np.array( | ||
timeit.Timer( | ||
stmt=statement, | ||
setup=setup, | ||
).repeat(7, 3) | ||
) | ||
/ 3 | ||
) | ||
return round(min(results), 5) | ||
|
||
|
||
if 1 in BENCHMARKS: | ||
print( | ||
"Polars-business: ", | ||
time_it("result_pl = df.select(plb.col('end_date').bdt.sub('start_date'))"), | ||
) | ||
print("NumPy: ", time_it("result_np = np.busday_count(start_dates, end_dates)")) |
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 |
---|---|---|
@@ -1,7 +1,20 @@ | ||
import polars as pl | ||
import numpy as np | ||
import polars_business as plb | ||
from datetime import date | ||
|
||
df = pl.DataFrame({"ts": [date(2020, 1, 1)]}) | ||
|
||
print(df.with_columns(ts_shifted=plb.col("ts").bdt.offset_by('3bd'))) | ||
df = pl.DataFrame( | ||
{ | ||
"start": pl.date_range(date(2019, 12, 30), date(2020, 2, 8), eager=True), | ||
"end": [date(2020, 2, 3)] * 41, | ||
} | ||
) | ||
with pl.Config(tbl_rows=100): | ||
print( | ||
df.with_columns( | ||
start_weekday=pl.col("start").dt.weekday(), | ||
end_weekday=pl.col("end").dt.weekday(), | ||
result=plb.col("end").bdt.sub("start", weekend=("Fri",)), | ||
result_np=pl.Series(np.busday_count(df["start"], df["end"])), | ||
) | ||
) |
Oops, something went wrong.