core_lens.utils.season#
Season-to-date-range resolution for time filtering and temporal column derivation.
Functions#
|
Convert a |
|
Return df with temporal grouping columns appended. |
Module Contents#
- core_lens.utils.season.resolve_time_filter(time_filter: dict[str, str | int | tuple[int, int] | None], time_col: str, season_config: core_lens.aoi.SeasonConfig) polars.Expr#
Convert a
View.time_filterdict to a Polars filter expression.Two modes are supported, matching the dict shapes produced by
between():Date range mode —
{"start": "YYYY-MM-DD", "end": "YYYY-MM-DD"}. Translated directly to abetweenpredicate ontime_col.Season mode —
{"season": name, "year": int | (int, int)}. The season name is resolved to a(MM-DD, MM-DD)range fromseason_config. Year-crossing seasons (e.g. rabi: Nov–Mar) produce anORexpression. Whenyearis absent, all years are matched. Whenyearis a single integer, only that year (or the transition year for year-crossing seasons) is matched.- Parameters:
time_filter (dict[str, str | int | tuple[int, int] | None]) – The dictionary stored on
time_filter.time_col (str) – Name of the time column in the Parquet file.
season_config (SeasonConfig) – The
SeasonConfigin effect.
- Returns:
A Polars expression that can be passed to
.filter().- Return type:
pl.Expr
- Raises:
ValueError – If
time_filterhas an unrecognised structure.
- core_lens.utils.season.add_temporal_columns(df: polars.DataFrame | polars.LazyFrame, time_col: str, season_config: core_lens.aoi.SeasonConfig) polars.DataFrame | polars.LazyFrame#
Return df with temporal grouping columns appended.
Adds the five columns that
aggregate()accepts asbyvalues. Called by the materialisation layer (_materialise()) immediately after collect for every sub_annualResult.Columns added (if not already present):
year— integer calendar year of the date.month— integer month (1–12).year_month— string"YYYY-MM"(zero-padded).season— season name string ("kharif"|"rabi"|"zaid").season_year— string"{season}_{year}"(e.g."kharif_2022").
If a column with one of those names already exists on df it is left untouched — this prevents overwriting data the entity itself may supply.
- Parameters:
df (pl.DataFrame | pl.LazyFrame) – The collected sub_annual DataFrame.
time_col (str) – Name of the date/datetime column to derive from.
season_config (SeasonConfig) – The
SeasonConfigin effect, used to map each date to its season name.
- Returns:
A new DataFrame with the temporal grouping columns appended.
- Return type:
pl.DataFrame | pl.LazyFrame