diff --git a/Cargo.toml b/Cargo.toml index 948320d..30420a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/utils/qdf/pivots.rs b/src/utils/qdf/pivots.rs index 7d6bbb3..ed110d0 100644 --- a/src/utils/qdf/pivots.rs +++ b/src/utils/qdf/pivots.rs @@ -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> { + 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) }