Downsampler¶
yohou.preprocessing.Downsampler
¶
Bases: BaseActualTransformer
Downsample time series to a lower frequency using aggregation.
Reduces the frequency of time series data by grouping consecutive time
points into bins and applying an aggregation function. Uses polars'
group_by_dynamic for efficient windowed aggregation.
Because group_by_dynamic bins by wall-clock windows, the input does not need a
uniform grid: Downsampler declares accepts_irregular_grid=True, so a jittered
or gapped sub-hourly feed is accepted at fit and transform (the strict
interval-consistency check is skipped and a representative median interval is
recorded for the target >= input guard). Behavior on a uniform grid is unchanged.
Accepting a gapped input axis means the output can carry gaps too: a window with
no rows produces no bin, so a gap in the input becomes a gap in the output. A
downstream transformer that requires a uniform grid may not notice, because the
strict interval check tolerates a sub-day delta spread and will infer an interval
from a gapped axis rather than reject it. A lag or rolling transformer placed after
a Downsampler on gapped input therefore computes over rows that are not the
real-time distance apart that its parameters imply. Fill or validate the gaps
(see SimpleTimeImputer, Upsampler) before an order-dependent step.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
str
|
Target time interval (e.g., "1h", "1d", "5m", "30s"). Uses polars duration string syntax. Must be greater than or equal to the input data's interval. |
'1h'
|
aggregation
|
(mean, sum, min, max, first, last, median)
|
Aggregation function to apply within each time bin: - "mean": Average values in each bin - "sum": Sum values in each bin - "min": Minimum value in each bin - "max": Maximum value in each bin - "first": First value in each bin - "last": Last value in each bin - "median": Median value in each bin |
"mean"
|
closed
|
(left, right)
|
Which side of the interval is closed. |
"left"
|
label
|
(left, right)
|
Which side of the interval to use as the label for each bin. |
"left"
|
include_boundaries
|
bool
|
Whether to include the interval boundaries in output. |
False
|
Attributes ¶
| Name | Type | Description |
|---|---|---|
n_features_in_ |
int
|
Number of features seen during fit. |
feature_names_in_ |
list of str
|
Names of features seen during fit. |
input_interval_ |
timedelta or None
|
Detected time interval of input data. |
target_interval_ |
timedelta or None
|
Target time interval. |
Examples ¶
>>> import polars as pl
>>> from datetime import datetime, timedelta
>>> from yohou.preprocessing import Downsampler
>>> # Create hourly data
>>> times = [datetime(2020, 1, 1) + timedelta(hours=i) for i in range(24)]
>>> X = pl.DataFrame({"time": times, "value": list(range(24))})
>>> # Downsample to daily (24h) using mean aggregation
>>> downsampler = Downsampler(interval="1d", aggregation="mean")
>>> downsampler.fit(X)
Downsampler(interval='1d')
>>> X_daily = downsampler.transform(X)
>>> len(X_daily) == 1 # Single day
True
See Also ¶
Upsampler : Upsample time series to higher frequency.
Source Code ¶
Source code in src/yohou/preprocessing/resampling.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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 | |
Methods ¶
get_feature_names_out(input_features=None)
¶
Get output feature names for transformation.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
input_features
|
list of str or None
|
Column names of the input features. If |
None
|
Returns ¶
| Type | Description |
|---|---|
list of str
|
Output feature names after transformation. |
Source Code ¶
Source code in src/yohou/preprocessing/resampling.py
Tutorials¶
The following example notebooks use this component:
-
How to Handle Long Series
Limit history with observation_horizon, weight recent errors with exponential decay, and downsample high-frequency data.
-
How to Resample Time Series
Demonstrate Downsampler and Upsampler for changing time series frequency, including multivariate support, boundary settings, and round-trip information loss.