I’m having a hard time converting a Series of strings into an array to iterate in a for loop.
Each item in the Series should be passed to the lit expression, to limit the DataFrame on the specific date.
Then, the DataFrames are converted into an ndarray for matrix alg, then passed into a new DataFrame that is pushed & collected into a Vec.
Here is what I’m trying to do when trying to iterate let dates = &unique_dates["date"];
This will return an error on lit(date)
which requires a bound (not being passed as a string).
When printing the first iteration, this will return String("2023-02-02")
instead of "2023-02-02"
#[pyfunction]
fn factor_returns(pydf: PyDataFrame, endog: Vec) -> PyResult {
let endog_str: Vec<&str> = endog.iter().map(|s| s.as_str()).collect();
let df: DataFrame = pydf.into();
let unique_dates = df.clone()
.lazy()
.select([col("date").unique()])
.collect()
.expect("could not collect");
// let dates = ["2022-12-01","2022-11-11"];
let dates = &unique_dates["date"];
for date in dates.iter(){
println!(“{:?}”, date);
let df_date = df.clone()
.lazy()
.filter(col("date").eq(lit(date)))
.select([
cols(&endog_str),
col("log_returns"),
])
.collect()
.expect("could not collect");
When passing an array of dates using `let dates = ["2022-12-01","2022-11-11"];` will work fine and return the proper result.
Here is a similar post to convert PolarsNumericType into a vector
/questions/71376935/how-to-get-a-vec-from-polars-series-or-chunkedarray
Here is what the Polars Series returns.
shape: (781,)
Series: ‘date’ [str]
[
“2023-02-02”
“2022-11-17”
“2022-03-08”
“2021-06-24”
“2022-04-13”
…
“2021-01-15”
“2023-08-24”
“2022-10-07”
“2021-08-16”
“2023-01-10”
]