Time aggregation · time_agg
---
config:
flowchart:
useMaxWidth: false
---
flowchart TB
classDef ok fill:#d4edda,stroke:#28a745,color:#000
classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
O["Time aggregation · time_agg<br/>🟢8"]:::ok
subgraph G_ds["Dataset"]
direction LR
v_ds["time_agg(p, DS)"]:::ok
end
subgraph G_comp["Component"]
direction LR
v_group_all["sum(DS group all time_agg(p))"]:::ok
v_comp["calc time_agg(p, comp)"]:::ok
end
subgraph G_sc["Scalar"]
direction LR
v_sc_time_period["time_agg(p, sc) on time_period"]:::ok
v_sc_date["time_agg(p, sc) on date"]:::ok
v_period_from["time_agg(p, from, sc)"]:::ok
v_first["time_agg(p, sc, first) on date"]:::ok
v_last["time_agg(p, sc, last) on date"]:::ok
end
subgraph G_use["Used with"]
direction LR
used__aggregate__aggregate_invocation["Aggregate invocation ↗<br/><small>used in group all</small>"]:::ext
used__clause__aggr["aggr ↗<br/><small>used in group all</small>"]:::ext
end
O --- G_ds
O --- G_comp
O --- G_sc
O -.- G_use
style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
style G_sc fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 2 3
style G_use fill:#ffffff,stroke:#868e96,stroke-width:1px,stroke-dasharray: 1 4
click v_group_all href "#form-group-all"
click v_ds href "#form-ds"
click v_comp href "#form-comp"
click v_sc_time_period href "#form-sc-time-period"
click v_sc_date href "#form-sc-date"
click v_period_from href "#form-period-from"
click v_first href "#form-first"
click v_last href "#form-last"
click used__aggregate__aggregate_invocation href "../aggregate/aggregate_invocation.html" "Aggregate invocation · group by"
click used__clause__aggr href "../clause/aggr.html" "Aggregation · aggr"
Types
Takes time, date, time_period, duration · Gives time, date, time_period
Comes from · time, date, time_period, duration — 10 operators in 2 blocks 🟢10
General purpose operators (1) 🟢1
Time operators (9) 🟢9
Comes from · operators that return any type (their input's) — 36 operators in 8 blocks 🟢36
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Goes to · time, date, time_period — 13 operators in 3 blocks 🟢13
General purpose operators (1) 🟢1
Time operators (11) 🟢11
period_indicatorFill time series fill_time_seriesFlow to stock flow_to_stockStock to flow stock_to_flowTime shift timeshiftDays between two dates datediffAdd time unit to date dateaddExtract the year getyearExtract the month getmonthExtract the day of the month dayofmonthExtract the day of the year dayofyearDefinitions (VTL-DL) (1) 🟢1
Goes to · operators that take any scalar — 49 operators in 9 blocks 🟢49
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (10) 🟢10
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Definitions (VTL-DL) (1) 🟢1
Forms
time_agg(p, DS) 🟢 · time, duration → time
Monthly measure to quarters 🟢 pandas ✅ · DuckDB ✅
- Tests:
time_agg("Q", DS_1)on a dataset with a monthly time_period measureMe_1and a quarterly time_period identifierId_1: the measure is converted, not the identifier.- Manual:
Time aggregation: “The Data Set version acts on a single Measure Data Set of type time, date or time_period and returns a Data Set having the same structure”. Its Additional Constraint asks for “exactly one Identifier” of those types, where VTL 2.1 said “exactly one measure”:
DS_1meets both, and the example follows the Behaviour.- Expected:
2019M02,2019M11and2019M07go to2019Q1,2019Q4and2019Q3, the input duration being “inferred from the internal representation of the value”; converting the quarterlyId_1instead would leave the months inMe_1.
DS_r := time_agg("Q", DS_1);
| Id_1 | Me_1 |
|---|---|
| 2020Q1 | 2019M02 |
| 2020Q2 | 2019M11 |
| 2020Q3 | 2019M07 |
| Id_1 | Me_1 |
|---|---|
| 2020Q1 | 2019Q1 |
| 2020Q2 | 2019Q4 |
| 2020Q3 | 2019Q3 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
sum(DS group all time_agg(p)) 🟢 · time, duration → time
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
sum(DS_1 group all time_agg("A")): the quarters of 2010 summed by year,time_aggused with an aggregation and no operand.- Manual:
Time aggregation, Example 1, its data points verbatim: “VTL also provides a component version, for use in combination with an aggregation operator, because the change of frequency requires an aggregation.”
- Expected:
2010with 60, 100 and 20 forA,BandC.Me_1may be Integer or Number: Sum declares a number, but “implementations may preserve the integer type for the resulting measure or component when all operand values are integer”; the manual’s output writes Integer, one of the two.
DS_r := sum ( DS_1 group all time_agg ( "A" ));
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 2010Q1 | A | 20 |
| 2010Q2 | A | 20 |
| 2010Q3 | A | 20 |
| 2010Q1 | B | 50 |
| 2010Q2 | B | 50 |
| 2010Q1 | C | 10 |
| 2010Q2 | C | 10 |
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 2010 | A | 60 |
| 2010 | B | 100 |
| 2010 | C | 20 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
calc time_agg(p, comp) 🟢 · time, duration → time
Mixed periods to years 🟢 pandas ✅ · DuckDB ✅
- Tests:
time_agg("A", Me_1)insidecalc, as the page’sds_2 := ds_1[calc Me1 := time_agg(“M”,Me1)], on a quarter, a month and a semester.- Manual:
Time aggregation: “On time_period type, the operator maps the input value into the comprising larger time period specified by the periodIndTo parameter”, and the input duration “can be inferred from the internal representation of the value”.
- Expected:
Each value goes to its year, whatever its own indicator:
2010Q2,2011M07and2012S2give2010,2011and2012, a Time_PeriodMe_2, as the operator “returns a scalar of the corresponding type”.
DS_r := DS_1[calc Me_2 := time_agg("A", Me_1)];
| Id_1 | Me_1 |
|---|---|
| 1 | 2010Q2 |
| 2 | 2011M07 |
| 3 | 2012S2 |
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 2010Q2 | 2010 |
| 2 | 2011M07 | 2011 |
| 3 | 2012S2 | 2012 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
time_agg(p, sc) on time_period 🟢 · time_period, duration → time_period
Month to quarter 🟢 pandas ✅ · DuckDB ✅
- Tests:
time_agg("Q", SC_1)withSC_1= 2012M01: January 2012 to its quarter, the page’s Example 2 without the cast mask, the scalar being an input because VTL has no time_period literal.- Manual:
Time aggregation, Example 2:
DS_r := time_agg ( "Q", cast ( "2012M01", time_period, "YYYY\\MMM" ) );“returns “2012Q1””.- Expected:
DS_r = 2012Q1, a Time_Period: without the mask, the manual’s default conversion from string to time_period (Type conversion) reads2012M01as January 2012.
DS_r := time_agg("Q", SC_1);
| SC_1 |
|---|
| 2012M01 |
Name |
Type |
Value |
|---|---|---|
|
Time_Period |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗
time_agg(p, sc) on date 🟢 · date, duration → date
Without first or last 🟢 pandas ✅ · DuckDB ✅
- Tests:
time_agg("M", SC_1)withSC_1= 2012-12-23, withoutfirstorlast: the page’s valid syntaxtime_agg(“M”, cast (“2012-12-23”, date, “YYYY-MM-DD”) )with the date as an input instead of the cast, whose mask the default conversion does not need.- Manual:
Time aggregation: “On date type, the operator maps the input value into the comprising larger period, whose duration is the one specified by the periodIndTo parameter, which is conventionally represented either by the start or by the end date, according to the first/last parameter”; it states no default for that parameter.
- Expected:
the error
1-1-19-11, “At op time_agg: If used over a Date, first/last parameter must be declared”: the page represents the period of a date “according to the first/last parameter” and gives no default, though its examples of valid syntaxes have this call.
DS_r := time_agg("M", SC_1);
| SC_1 |
|---|
| 2012-12-23 |
Expected error 1-1-19-11: At op time_agg: If used over a Date, first/last parameter must be declared.
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/error.json · example page ↗
time_agg(p, from, sc) 🟢 · time, duration → time
Quarter to year 🟢 pandas ✅ · DuckDB ✅
- Tests:
time_agg("A", "Q", SC_1)withSC_1= 2012Q2: the optionalperiodIndFromgiven,Q, the duration of the operand.- Manual:
Time aggregation: the syntax
time_agg ( periodIndTo { , periodIndFrom } { , op } { , first | last } ), and “It is only possible to convert smaller duration values to larger duration values”, as a quarter to a year is.- Expected:
DS_r = 2012, a Time_Period: the second quarter of 2012 is in the year 2012.
DS_r := time_agg("A", "Q", SC_1);
| SC_1 |
|---|
| 2012Q2 |
Name |
Type |
Value |
|---|---|---|
|
Time_Period |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗
time_agg(p, sc, first) on date 🟢 · date, duration → date
Start of the year 🟢 pandas ✅ · DuckDB ✅
- Tests:
time_agg("A", SC_1, first)withSC_1= 2012-02-13: a date to its year, represented by its start, on the date of the page’s Example 3.- Manual:
Time aggregation, Example 4: “The following example maps a date to year level, 2012 (beginning of the period)” and “produces a date value corresponding to the string “20120101””. Its script,
DS_r := time_agg(cast( “A”, “2012M1”, date, “YYYYMMDD”), first );, is garbled, with the indicator inside the cast and2012M1not matching the mask: the example writes the call that the text describes.- Expected:
DS_r = 2012-01-01, a Date: with first, the year 2012 is “represented […] by the start”.
DS_r := time_agg("A", SC_1, first);
| SC_1 |
|---|
| 2012-02-13 |
Name |
Type |
Value |
|---|---|---|
|
Date |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗
time_agg(p, sc, last) on date 🟢 · date, duration → date
End of the quarter 🟢 pandas ✅ · DuckDB ✅
- Tests:
time_agg("Q", SC_1, last)withSC_1= 2012-02-13: a date to its quarter, represented by its end; the page’s Example 3, with the date as an input in the defaultYYYY-MM-DDform instead of the cast with the maskYYYYMMDD.- Manual:
Time aggregation, Example 3: “The following example maps a date to quarter level, 2012 (end of the period)”,
DS_r := time_agg( “Q”, cast(“20120213”, date, “YYYYMMDD”), last );, “and produces a date value corresponding to the string “20120331””.- Expected:
DS_r = 2012-03-31, a Date: 2012-02-13 is in the first quarter of 2012, which ends on March 31.
DS_r := time_agg("Q", SC_1, last);
| SC_1 |
|---|
| 2012-02-13 |
Name |
Type |
Value |
|---|---|---|
|
Date |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗