select_panel_columns¶
yohou.utils.panel.select_panel_columns(df, groups, include_global=True)
¶
Select panel group columns and optionally global columns of a DataFrame.
For panel data (DataFrames with columns using __ separator for groups), this function filters columns to keep only the "time" column, columns matching any of the panel group prefixes, and optionally global columns.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame with potential mix of global and group columns. Must contain a "time" column. |
required |
groups
|
list of str or None
|
List of all group prefixes in the dataset. All columns matching
any |
required |
include_global
|
bool
|
Whether to keep global columns (without __) in addition to time and panel group columns. - True: Keep time + all panel groups + all global columns for X - False: Keep only time + all panel groups (for y target data) |
True
|
Returns¶
| Type | Description |
|---|---|
DataFrame
|
Filtered DataFrame containing "time", columns matching any panel group prefix, and optionally global columns. |
Examples¶
>>> import polars as pl
>>> # Panel data with group columns and global column
>>> df = pl.DataFrame({
... "time": [1, 2, 3],
... "global_feature": [10.0, 20.0, 30.0],
... "sales__store_1": [100, 110, 120],
... "sales__store_2": [150, 160, 170],
... "inventory__store_1": [50, 55, 60],
... "inventory__store_2": [75, 80, 85],
... })
>>> # Filter for target (y) - exclude global features
>>> y_filtered = select_panel_columns(df, ["sales", "inventory"], include_global=False)
>>> set(y_filtered.columns) == {
... "time",
... "sales__store_1",
... "sales__store_2",
... "inventory__store_1",
... "inventory__store_2",
... }
True
>>> # Filter for features (X) - include global features
>>> X_filtered = select_panel_columns(df, ["sales", "inventory"], include_global=True)
>>> set(X_filtered.columns) == {
... "time",
... "global_feature",
... "sales__store_1",
... "sales__store_2",
... "inventory__store_1",
... "inventory__store_2",
... }
True
See Also¶
inspect_panel: Inspect DataFrame to identify global and local columns
Source Code¶
Show/Hide source
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 309 310 311 312 313 | |