CalendarFeatureTransformer¶
yohou.preprocessing.CalendarFeatureTransformer
¶
Bases: BaseActualTransformer
Extract calendar-based features from the time column.
Creates new integer feature columns derived from the datetime index,
useful for capturing seasonal and calendar effects in reduction
forecasters. Output columns are prefixed with cal_.
Each feature is a deterministic function of the timestamp \(t\):
For example, \(f_{\text{month}}(t) \in \{1, \ldots, 12\}\) and \(f_{\text{is_weekend}}(t) \in \{0, 1\}\).
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
features
|
list of str or None
|
Calendar features to extract. If |
None
|
time_zone
|
str or None
|
If set to an IANA zone, features are computed from the |
None
|
Attributes ¶
| Name | Type | Description |
|---|---|---|
applicable_features_ |
list of str
|
Calendar features that will be extracted during transform. |
Raises ¶
| Type | Description |
|---|---|
ValueError
|
At fit time if any requested feature name is not a valid calendar
feature; if a requested feature is not applicable to the detected time
interval (e.g. |
See Also ¶
HolidayFeatureTransformer: Binary holiday indicator from user-provided dates.DaylightSavingFeatureTransformer: Daylight-saving offset and transition-day features.FourierFeatureTransformer: Sin/cos harmonics for cyclical encoding.TimeIndexTransformer: Numeric time index for trend features.FunctionTransformer: Custom function-based transforms.
Examples ¶
>>> import polars as pl
>>> from datetime import datetime
>>> time = pl.datetime_range(
... start=datetime(2020, 1, 1), end=datetime(2020, 3, 1), interval="1d", eager=True
... )
>>> X = pl.DataFrame({"time": time, "value": range(len(time))})
>>> transformer = CalendarFeatureTransformer(features=["month", "day_of_week"])
>>> transformer.fit(X)
CalendarFeatureTransformer(features=['month', 'day_of_week'])
>>> X_t = transformer.transform(X)
>>> "cal_month" in X_t.columns
True
With time_zone set, cal_hour is local while the output "time" stays put:
>>> from datetime import timezone
>>> t = pl.datetime_range(
... datetime(2026, 7, 1, 17, tzinfo=timezone.utc),
... datetime(2026, 7, 1, 18, tzinfo=timezone.utc),
... interval="1h",
... eager=True,
... )
>>> Xz = pl.DataFrame({"time": t})
>>> out = CalendarFeatureTransformer(features=["hour"], time_zone="America/Chicago").fit_transform(Xz)
>>> out["cal_hour"].to_list() # UTC 17, 18 -> Central 12, 13
[12, 13]
>>> out["time"].to_list() == Xz["time"].to_list() # output time unchanged
True
Source Code ¶
Source code in src/yohou/preprocessing/calendar.py
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 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 | |
Methods ¶
get_feature_names_out(input_features=None)
¶
Get output feature names for transformation.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
input_features
|
array-like of str or None
|
Input feature names (unused, for API compatibility). |
None
|
Returns ¶
| Type | Description |
|---|---|
list of str
|
All non-time output column names. |
Source Code ¶
Source code in src/yohou/preprocessing/calendar.py
Tutorials¶
The following example notebooks use this component:
-
How to Add Calendar, Fourier, and Holiday Features
Enrich your feature matrix with time-derived signals using CalendarFeatureTransformer, FourierFeatureTransformer, and HolidayFeatureTransformer.