window_forecasts¶
yohou.utils.pivot.window_forecasts(X_forecast, observation_times, forecasting_horizon, interval, *, vintage_col='vintage_time', time_col='time')
¶
Window forecast data into step-indexed columns using as-of vintage selection.
For each observation time T, selects the latest vintage V where V <= T
(as-of / backward match), then extracts forecast values at
T + 1*interval through T + H*interval from that vintage's rows.
The result is a wide DataFrame with step columns aligned to observation
times, not vintage times.
Steps are aligned to the observation time, making this function suitable for training with sparse vintage schedules (e.g., 6-hourly weather forecasts with hourly observations).
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
X_forecast
|
DataFrame
|
Tidy forecast DataFrame with |
required |
observation_times
|
Series
|
Series of observation timestamps. One output row per observation time. |
required |
forecasting_horizon
|
int
|
Number of forward steps (H) to extract per observation time. |
required |
interval
|
str or timedelta
|
Time frequency between steps (e.g., |
required |
vintage_col
|
str
|
Name of the column identifying when the forecast was issued. |
"vintage_time"
|
time_col
|
str
|
Name of the column identifying the target time of each forecast value. |
"time"
|
Returns¶
| Type | Description |
|---|---|
DataFrame
|
Wide DataFrame with |
Raises¶
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
ValueError
|
If no value columns remain after removing |
Examples¶
>>> import polars as pl
>>> from datetime import datetime
>>> X_forecast = pl.DataFrame({
... "vintage_time": [datetime(2020, 1, 1, 0)] * 3 + [datetime(2020, 1, 1, 6)] * 3,
... "time": [datetime(2020, 1, 1, 1), datetime(2020, 1, 1, 2), datetime(2020, 1, 1, 3)] * 2,
... "temp": [10.0, 11.0, 12.0, 15.0, 16.0, 17.0],
... })
>>> obs = pl.Series([datetime(2020, 1, 1, 0), datetime(2020, 1, 1, 3)])
>>> window_forecasts(X_forecast, obs, forecasting_horizon=2, interval="1h")
shape: (2, 3)
┌─────────────────────┬─────────────┬─────────────┐
│ time ┆ temp_step_1 ┆ temp_step_2 │
│ --- ┆ --- ┆ --- │
│ datetime[μs] ┆ f64 ┆ f64 │
╞═════════════════════╪═════════════╪═════════════╡
│ 2020-01-01 00:00:00 ┆ 10.0 ┆ 11.0 │
│ 2020-01-01 03:00:00 ┆ null ┆ null │
└─────────────────────┴─────────────┴─────────────┘
Source Code¶
Show/Hide source
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |