# forecast_with_enriched_df.py
import asyncio
import numpy as np
import pandas as pd
from synthefy.api_client import SynthefyAsyncAPIClient, SynthefyAPIClient
from synthefy.synthefy_helpers import get_weather_data
async def main():
# Load sample data
df = pd.read_csv("walmart.csv")
df["Date"] = pd.to_datetime(df["Date"])
df = df[df["Store"] == "store_1"] # Use one store for simplicity
# Split into history vs. future target
cutoff_date = '2012-11-02'
history_df = df[df['Date'] <= cutoff_date].copy()
future_dates = pd.date_range('2012-11-02', periods=7, freq='D')
target_df = pd.DataFrame({
'Date': future_dates,
'Weekly_Sales': np.nan, # unknown future target
})
# Enrich both frames with weather data
base_client = SynthefyAPIClient()
# Step 1: Enrich data with weather
enriched_history = get_weather_data(
client=base_client,
location_name="Springfield",
weather_parameters="basic",
user_dataframe=history_df,
user_timestamp_column="Date",
auto_select_location=True,
)
enriched_target = get_weather_data(
client=base_client,
location_name="Springfield",
weather_parameters="basic",
user_dataframe=target_df,
user_timestamp_column="Date",
auto_select_location=True,
)
# Step 2: Get weather column names for covariates
added_hist = [c for c in enriched_history.columns if c not in history_df.columns]
added_targ = [c for c in enriched_target.columns if c not in target_df.columns]
metadata_cols = sorted(list(set(added_hist).intersection(added_targ)))
# Step 3: Generate forecast with enriched data
async with SynthefyAsyncAPIClient() as client:
forecast_dfs = await client.forecast_dfs(
history_dfs=[enriched_history],
target_dfs=[enriched_target],
target_col='Weekly_Sales',
timestamp_col='Date',
metadata_cols=metadata_cols,
leak_cols=[],
model='sfm-moe-v1'
)
forecast_df = forecast_dfs[0]
print(forecast_df.head())
# Run
asyncio.run(main())