mirror of
https://github.com/Magnus167/msyrs.git
synced 2025-11-23 09:36:15 +00:00
Add logging for historic volatility calculations and implement utility to get min/max real dates
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use chrono::NaiveDate;
|
||||
use polars::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
@@ -11,6 +12,35 @@ pub fn split_ticker(ticker: String) -> Result<(String, String), Box<dyn Error>>
|
||||
Ok((parts[0].to_string(), parts[1].to_string()))
|
||||
}
|
||||
|
||||
pub fn get_min_max_real_dates(
|
||||
df: &DataFrame,
|
||||
date_col: &str,
|
||||
) -> Result<(NaiveDate, NaiveDate), Box<dyn Error>> {
|
||||
let date_series = df.column(date_col)?;
|
||||
if let DataType::Date = date_series.dtype() {
|
||||
// Convert the `date` series to an i32 (days since 1970-01-01)
|
||||
let date_as_days = date_series.cast(&DataType::Int32)?;
|
||||
let min_days = date_as_days.i32()?.min().ok_or("No minimum value found")?;
|
||||
let max_days = date_as_days.i32()?.max().ok_or("No maximum value found")?;
|
||||
|
||||
// Convert the days back to `NaiveDate`
|
||||
let min_date = NaiveDate::from_ymd_opt(1970, 1, 1)
|
||||
.unwrap()
|
||||
.checked_add_signed(chrono::Duration::days(min_days as i64))
|
||||
.ok_or("Invalid minimum date")?;
|
||||
let max_date = NaiveDate::from_ymd_opt(1970, 1, 1)
|
||||
.unwrap()
|
||||
.checked_add_signed(chrono::Duration::days(max_days as i64))
|
||||
.ok_or("Invalid maximum date")?;
|
||||
|
||||
Ok((min_date, max_date))
|
||||
} else {
|
||||
Err(Box::new(polars::error::PolarsError::ComputeError(
|
||||
"The column is not of Date type".into(),
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn get_cid(ticker: String) -> Result<String, Box<dyn Error>> {
|
||||
split_ticker(ticker).map(|(cid, _)| cid)
|
||||
|
||||
Reference in New Issue
Block a user