mirror of
https://github.com/Magnus167/msyrs.git
synced 2025-08-20 07:20:01 +00:00
feat: update Cargo.toml and enhance pivot functionality in QDF
This commit is contained in:
parent
99a33237d6
commit
3ffab4c95d
@ -9,8 +9,8 @@ rustdoc = ["--cfg", "doc"]
|
||||
|
||||
[lib]
|
||||
name = "msyrs"
|
||||
# path = "src/lib.rs"
|
||||
crate-type = ["cdylib"]
|
||||
path = "src/lib.rs"
|
||||
crate-type = ["cdylib", "lib"]
|
||||
|
||||
|
||||
[dependencies]
|
||||
|
@ -10,6 +10,15 @@ use std::f32::consts::E;
|
||||
/// The required columns for a Quantamental DataFrame.
|
||||
const QDF_INDEX_COLUMNS: [&str; 3] = ["real_date", "cid", "xcat"];
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn get_ticker_from_qdf(df: &DataFrame) -> Result<String, Box<dyn Error>> {
|
||||
check_quantamental_dataframe(df)?;
|
||||
let cid = df.column("cid")?.get(0).unwrap().to_string();
|
||||
let xcat = df.column("xcat")?.get(0).unwrap().to_string();
|
||||
let ticker = format!("{}_{}", cid, xcat);
|
||||
Ok(ticker)
|
||||
}
|
||||
|
||||
/// Pivots a dataframe to a format where each ticker a column.
|
||||
#[allow(dead_code)]
|
||||
pub fn pivot_dataframe_by_ticker(
|
||||
@ -27,7 +36,8 @@ pub fn pivot_dataframe_by_ticker(
|
||||
// set metric to the first non-index column
|
||||
metric = df.get_column_names()[3].to_string();
|
||||
}
|
||||
let unique_dates: Column = get_unique_dates(&df)?;
|
||||
let mut unique_dates: Column = get_unique_dates(&df)?;
|
||||
unique_dates.rename("real_date".into());
|
||||
|
||||
// keep only the index columns and the metric column
|
||||
let mut keep_cols = QDF_INDEX_COLUMNS
|
||||
@ -38,18 +48,21 @@ pub fn pivot_dataframe_by_ticker(
|
||||
keep_cols.push(metric.clone());
|
||||
|
||||
let mut out_dfs = split_df_by_tickers(&df, Some(vec![metric.clone()]))?;
|
||||
for (_, odf) in out_dfs.iter_mut() {
|
||||
// select keep_cols
|
||||
*odf = odf.select(vec!["real_date", &*metric.clone()])?;
|
||||
for (tkr, odf) in out_dfs.iter_mut() {
|
||||
odf.rename(&metric, tkr.into())?;
|
||||
*odf = odf.select(vec!["real_date", tkr])?;
|
||||
}
|
||||
|
||||
// create a new dataframe with the unique dates, and iteratively add add the metric columns
|
||||
// create a new dataframe with the unique dates
|
||||
|
||||
let mut new_df = DataFrame::new(vec![unique_dates])?;
|
||||
|
||||
for (_, odf) in out_dfs.iter() {
|
||||
for (_, odf) in out_dfs.iter_mut() {
|
||||
new_df = new_df.left_join(odf, ["real_date"], ["real_date"])?;
|
||||
}
|
||||
|
||||
// left join all dataframes on the date column
|
||||
|
||||
Ok(new_df)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user