-
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.
- Loading branch information
1 parent
a8b0432
commit eb7878f
Showing
5 changed files
with
39 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
use polars::prelude::*; | ||
use pyo3_polars::derive::polars_expr; | ||
|
||
use bdays::{calendars::WeekendsOnly, HolidayCalendar}; | ||
use chrono::{NaiveDate, NaiveDateTime, Datelike}; | ||
use std::cmp::min; | ||
|
||
// cool, seems to work! let's try it with Datetime? | ||
|
||
#[polars_expr(output_type=Date)] | ||
fn add_bday(inputs: &[Series]) -> PolarsResult<Series> { | ||
let ca = inputs[0].date()?; | ||
Ok(ca.clone().into_series()) | ||
} | ||
let ca = inputs[0].i32()?; | ||
let n = inputs[1].i32()?.get(0).unwrap(); | ||
|
||
// wow, this is super-slow, keeps repeatedly converting | ||
// between NaiveDate and timestamp! | ||
// multiple times for each element! max once should be enough! | ||
// or twice if necessary. but not so many | ||
|
||
let out = ca.apply_values( | ||
|x|{ | ||
let w = (x / (3600 * 24 - 3)) % 7 + 1; | ||
let n_days = n + (n + w) / 5 * 2; | ||
x + n_days | ||
} | ||
); | ||
Ok(out.cast(&DataType::Date).unwrap().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 |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import polars as pl | ||
from expression_lib import Language | ||
from expression_lib import BusinessDayTools | ||
from datetime import date, datetime | ||
import numpy as np | ||
|
||
df = pl.DataFrame({ | ||
"names": ["Richard", "Alice", "Bob"], | ||
"dates": [1]*3, | ||
"dates2": [date(2020, 1, 1)]*3, | ||
"dates": pl.datetime_range(date(1, 1, 1), date(9999, 1, 1), eager=True), | ||
}) | ||
print(df) | ||
df = df.filter(pl.col('dates').dt.weekday() <6) | ||
|
||
print(df.with_columns(dates_plus_1=pl.col('dates2').language.add_bday())) | ||
print(df.with_columns(dates_shifted=pl.col('dates').bdt.add_bday(n=15))[20:28]) | ||
print(df.with_columns(dates_shifted=pl.Series(np.busday_offset(df['dates'], 15)))[20:28]) | ||
|
||
import pandas as pd | ||
dfpd = df.to_pandas() | ||
print((dfpd + pd.tseries.offsets.BusinessDay(15)).iloc[20:28]) |