use pandrs::dataframe::{DataFrame, DataFrameWindowExt};
use pandrs::error::Result;
use pandrs::series::Series;
#[allow(clippy::result_large_err)]
#[allow(clippy::result_large_err)]
fn main() -> Result<()> {
println!("=== Example of DataFrame Window Operations ===\n");
let df = create_sample_dataframe()?;
println!("Original DataFrame:");
println!("{df:?}\n");
println!("1. Example of rolling mean");
let df_rolling = df.rolling(3, "Price", "mean", None)?;
println!("{df_rolling:?}\n");
println!("2. Example of rolling sum");
let df_sum = df.rolling(3, "Quantity", "sum", Some("Quantity_RollingSum_3"))?;
println!("{df_sum:?}\n");
println!("3. Example of rolling standard deviation");
let df_std = df.rolling(3, "Price", "std", None)?;
println!("{df_std:?}\n");
println!("4. Example of rolling minimum");
let df_min = df.rolling(3, "Price", "min", None)?;
println!("{df_min:?}\n");
println!("5. Example of rolling maximum");
let df_max = df.rolling(3, "Price", "max", None)?;
println!("{df_max:?}\n");
println!("6. Example of expanding mean");
let df_expanding = df.expanding(2, "Price", "mean", None)?;
println!("{df_expanding:?}\n");
println!("7. Example of Exponential Weighted Moving Average (EWM)");
let df_ewm_span = df.ewm("Price", "mean", Some(3), None, Some("Price_ewm_span3"))?;
println!("7.1 When span=3 is specified:");
println!("{df_ewm_span:?}\n");
let df_ewm_alpha = df.ewm("Price", "mean", None, Some(0.5), Some("Price_ewm_alpha0.5"))?;
println!("7.2 When alpha=0.5 is specified:");
println!("{df_ewm_alpha:?}\n");
println!("8. Example of combining multiple operations:");
let mut result_df = df.clone();
result_df = result_df.rolling(3, "Price", "mean", None)?;
result_df = result_df.expanding(2, "Price", "mean", None)?;
result_df = result_df.ewm("Price", "mean", Some(3), None, None)?;
println!("{result_df:?}\n");
println!("=== DataFrame Window Operations Example Complete ===");
Ok(())
}
#[allow(clippy::result_large_err)]
#[allow(clippy::result_large_err)]
fn create_sample_dataframe() -> Result<DataFrame> {
let mut df = DataFrame::new();
let dates = vec![
"2023-01-01",
"2023-01-02",
"2023-01-03",
"2023-01-04",
"2023-01-05",
"2023-01-06",
"2023-01-07",
"2023-01-08",
"2023-01-09",
"2023-01-10",
];
let date_series = Series::new(dates, Some("Date".to_string()))?;
df.add_column("Date".to_string(), date_series)?;
let products = vec![
"ProductA", "ProductB", "ProductA", "ProductC", "ProductB", "ProductA", "ProductC",
"ProductA", "ProductB", "ProductC",
];
let product_series = Series::new(products, Some("Product".to_string()))?;
df.add_column("Product".to_string(), product_series)?;
let prices = vec![
"100", "150", "110", "200", "160", "120", "210", "115", "165", "220",
];
let price_series = Series::new(prices, Some("Price".to_string()))?;
df.add_column("Price".to_string(), price_series)?;
let quantities = vec!["5", "3", "6", "2", "4", "7", "3", "8", "5", "4"];
let quantity_series = Series::new(quantities, Some("Quantity".to_string()))?;
df.add_column("Quantity".to_string(), quantity_series)?;
Ok(df)
}