Skip to main content
Version: 0.0.98

ts_shape.events.supplychain.demand_pattern

Demand pattern events for supply chain timeseries.

Aggregates demand by time window, detects demand spikes, and summarizes seasonal demand patterns.

Classes
NameDescription
DemandPatternEventsAnalyze demand patterns from a timeseries demand signal.

DemandPatternEvents​

classsource
DemandPatternEvents(
dataframe: pd.DataFrame,
demand_uuid: str,
*,
event_uuid: str = "sc:demand",
value_column: str = "value_double",
time_column: str = "systime"
) -> None

Bases: Base

Analyze demand patterns from a timeseries demand signal.

Each demand event is a numeric value representing demand quantity at a point in time.

Example usage

analyzer = DemandPatternEvents(df, demand_uuid='order_demand') daily = analyzer.demand_by_period(window='1D') spikes = analyzer.detect_demand_spikes(threshold_factor=2.0) seasonal = analyzer.seasonality_summary(window='1D')

Functions
NameDescription
demand_by_periodAggregate demand per time period.
detect_demand_spikesFlag periods where demand exceeds mean + threshold_factor * std.
seasonality_summaryCompute demand patterns by day-of-week (for daily) or hour-of-day (for hourly).
Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame with timeseries data.required
demand_uuidstrUUID of the demand signal.required
event_uuidstrUUID assigned to generated events.'sc:demand'
value_columnstrColumn containing numeric demand values.'value_double'
time_columnstrName of timestamp column.'systime'

demand_by_period​

methodsource
demand_by_period(
window: str = "1D", empty_periods: Literal["zero", "drop"] = "zero"
) -> pd.DataFrame

Aggregate demand per time period.

Each row of the demand signal is one order (or demand event), so a period without rows is a period without demand. With empty_periods="zero" (the default) every period between the first and the last period with data appears, the empty ones with total_demand = 0, peak_demand = 0, order_count = 0 and avg_demand missing (it is the mean order size, undefined without orders). A cumulative counter is not event-encoded demand: difference it into per-event quantities first.

Parameters
NameTypeDescriptionDefault
windowstrPandas offset alias for grouping (e.g. '1D', '1h').'1D'
empty_periodsLiteral['zero', 'drop']"zero" keeps periods without orders as zero demand; "drop" leaves them out (only periods with at least one order).'zero'
Returns
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds, uuid,
DataFramesource_uuid, is_delta, total_demand, avg_demand, peak_demand,
DataFrameorder_count. order_count is the number of orders (non-missing
DataFramevalues) in the period.
Raises
TypeDescription
ValueErrorIf empty_periods is not "zero" or "drop".

detect_demand_spikes​

methodsource
detect_demand_spikes(
threshold_factor: float = 2.0,
window: str = "1D",
empty_periods: Literal["zero", "drop"] = "zero",
) -> pd.DataFrame

Flag periods where demand exceeds mean + threshold_factor * std.

The mean and std are taken over the period totals of :meth:demand_by_period, by default including the periods without orders as zero demand: a spike is measured against the demand over the whole span, not only against the periods that had orders.

Parameters
NameTypeDescriptionDefault
threshold_factorfloatNumber of standard deviations above the mean to flag as a spike.2.0
windowstrPandas offset alias for aggregation period.'1D'
empty_periodsLiteral['zero', 'drop']Passed to :meth:demand_by_period; "drop" gives the baseline over periods with orders only.'zero'
Returns
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds, uuid,
DataFramesource_uuid, is_delta, demand, baseline_mean, spike_magnitude.
DataFrameend is start plus window.

seasonality_summary​

methodsource
seasonality_summary(
window: str = "1D", empty_periods: Literal["zero", "drop"] = "zero"
) -> pd.DataFrame

Compute demand patterns by day-of-week (for daily) or hour-of-day (for hourly).

When window='1D', groups by day of week (Monday=0 .. Sunday=6). When window is sub-daily (e.g. '1h'), groups by hour of day (0..23). The statistics are over the period totals of :meth:demand_by_period, by default including periods without orders as zero demand (a weekday that usually has no orders shows a low average).

Parameters
NameTypeDescriptionDefault
windowstrPandas offset alias. '1D' for day-of-week analysis, any sub-daily frequency for hour-of-day analysis.'1D'
empty_periodsLiteral['zero', 'drop']Passed to :meth:demand_by_period; "drop" uses periods with orders only.'zero'
Returns
TypeDescription
DataFrameDataFrame with columns: period_label, avg_demand, std_demand,
DataFramemin_demand, max_demand.