Stock to flow · stock_to_flow
---
config:
flowchart:
useMaxWidth: false
---
flowchart LR
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["Stock to flow · stock_to_flow<br/>🟢3"]:::ok
v_time_period["stock_to_flow(DS) on time_period"]:::ok
v_date["stock_to_flow(DS) on date"]:::ok
v_time["stock_to_flow(DS) on time"]:::ok
v_time_period --- O
v_date --- O
O --- v_time
click v_time_period href "#form-time-period"
click v_date href "#form-date"
click v_time href "#form-time"
Types
Takes number, time, date, time_period · Gives number, time, date, time_period
Comes from · number, time, date, time_period — 49 operators in 6 blocks 🟢49
General purpose operators (2) 🟢2
String operators (3) 🟢3
Numeric operators (18) 🟢18
Time operators (13) 🟢13
fill_time_seriesFlow to stock flow_to_stockTime shift timeshiftTime aggregation time_aggActual time current_dateDays 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 dayofyearYears and days to number of days yeartodayMonths and days to number of days monthtodayAggregate and Analytic operators (10) 🟢10
Data validation operators (3) 🟢3
Comes from · operators that return any type (their input's) — 34 operators in 7 blocks 🟢34
General purpose operators (5) 🟢5
Join operators (4) 🟢4
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Conditional operators (3) 🟢3
Goes to · number, time, date, time_period — 43 operators in 7 blocks 🟢43
General purpose operators (1) 🟢1
Numeric operators (18) 🟢18
Time operators (11) 🟢11
period_indicatorFill time series fill_time_seriesFlow to stock flow_to_stockTime shift timeshiftTime aggregation time_aggDays 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 dayofyearHierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Data validation operators (2) 🟢2
Definitions (VTL-DL) (2) 🟢2
Goes to · operators that take any scalar — 48 operators in 8 blocks 🟢48
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
Forms
stock_to_flow(DS) on time_period 🟢 · time_period, number → time_period, number
Manual example 3 🟢 pandas ✅ · DuckDB ✅
- Tests:
stock_to_flow(DS_3)on annual time_period series.- Manual:
Stock to flow, Example 3, output verbatim: “for each time series, calculates the corresponding flow data by subtracting from the measure values of each regular period the corresponding measure values of the previous one.”
- Expected:
The first value of each series, then the differences:
A2.0, 5.0, -3.0, 9.0 andB4.0, -8.0, 0.0, 6.0, Numbers.
DS_r := stock_to_flow ( DS_3 );
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| A | 2010 | 2.0 |
| A | 2011 | 7.0 |
| A | 2012 | 4.0 |
| A | 2013 | 13.0 |
| B | 2010 | 4.0 |
| B | 2011 | -4.0 |
| B | 2012 | -4.0 |
| B | 2013 | 2.0 |
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| A | 2010 | 2.0 |
| A | 2011 | 5.0 |
| A | 2012 | -3.0 |
| A | 2013 | 9.0 |
| B | 2010 | 4.0 |
| B | 2011 | -8.0 |
| B | 2012 | 0.0 |
| B | 2013 | 6.0 |
Files: script.vtl · input/DS_3.csv · input/DS_3.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
Manual example 4 🟢 pandas ✅ · DuckDB ✅
- Tests:
stock_to_flow(DS_4)on an annual and a quarterly series of the same phenomenonA, each on its own.- Manual:
Stock to flow, Example 4, output verbatim: “for each time series, calculates the corresponding flow data by subtracting from the measure values of each regular period the corresponding measure values of the previous one.”
- Expected:
The years give 2.0, 7.0, 4.0, 13.0 and the quarters of 2010 give 2.0, -3.0, 7.0, -4.0.
DS_r := stock_to_flow ( DS_4 );
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| A | 2010 | 2.0 |
| A | 2011 | 9.0 |
| A | 2012 | 13.0 |
| A | 2013 | 26.0 |
| A | 2010Q1 | 2.0 |
| A | 2010Q2 | -1.0 |
| A | 2010Q3 | 6.0 |
| A | 2010Q4 | 2.0 |
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| A | 2010 | 2.0 |
| A | 2011 | 7.0 |
| A | 2012 | 4.0 |
| A | 2013 | 13.0 |
| A | 2010Q1 | 2.0 |
| A | 2010Q2 | -3.0 |
| A | 2010Q3 | 7.0 |
| A | 2010Q4 | -4.0 |
Files: script.vtl · input/DS_4.csv · input/DS_4.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
stock_to_flow(DS) on date 🟢 · date, number → date, number
Manual example 2 🟢 pandas ✅ · DuckDB ✅
- Tests:
stock_to_flow(DS_2)on annual date series, where “conventionally each period is identified by its last day”.- Manual:
Stock to flow, Example 2, output verbatim: “for each time series, calculates the corresponding flow data by subtracting from the measure values of each regular period the corresponding measure values of the previous one.”
- Expected:
The first value of each series, then the differences:
A2.0, 5.0, -3.0, 9.0 andB4.0, -8.0, 0.0, 6.0.
DS_r := stock_to_flow ( DS_2 );
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| A | 2010-12-31 | 2.0 |
| A | 2011-12-31 | 7.0 |
| A | 2012-12-31 | 4.0 |
| A | 2013-12-31 | 13.0 |
| B | 2010-12-31 | 4.0 |
| B | 2011-12-31 | -4.0 |
| B | 2012-12-31 | -4.0 |
| B | 2013-12-31 | 2.0 |
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| A | 2010-12-31 | 2.0 |
| A | 2011-12-31 | 5.0 |
| A | 2012-12-31 | -3.0 |
| A | 2013-12-31 | 9.0 |
| B | 2010-12-31 | 4.0 |
| B | 2011-12-31 | -8.0 |
| B | 2012-12-31 | 0.0 |
| B | 2013-12-31 | 6.0 |
Files: script.vtl · input/DS_2.csv · input/DS_2.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
stock_to_flow(DS) on time 🟢 · time, number → time, number
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
stock_to_flow(DS_1)on annual series whose reference time identifier is a time.- Manual:
Stock to flow, Example 1: the operator “calculates the corresponding flow data by subtracting from the measure values of each regular period the corresponding measure values of the previous one.”
- Expected:
The manual’s values:
A2.0, 5.0, -3.0, 9.0 andB4.0, -8.0, 0.0, 6.0, a NumberMe_1.- Erratum:
The manual’s input structure declares
Me_1as String, but the operand’s measures aremeasure<number>, the values are numbers, and the manual’s output and itsDS_2toDS_4have a NumberMe_1: the inputMe_1is Number.- Adaptation:
The page writes its time values in “the time format YYYY-MM/YYYY-MM”, as
2010M1/2010M12, which vtlengine cannot load (DataLoadError0-3-1-6on both engines). EachYYYYM1/YYYYM12of the input and the output is writtenYYYY-01-01/YYYY-12-31, the same interval in theYYYY-MM-DD/YYYY-MM-DDform of the manual’s “Behaviour of Time operators” and ofdocs/data_types.rst; nothing else changes.
DS_r := stock_to_flow ( DS_1 );
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| A | 2010-01-01/2010-12-31 | 2.0 |
| A | 2011-01-01/2011-12-31 | 7.0 |
| A | 2012-01-01/2012-12-31 | 4.0 |
| A | 2013-01-01/2013-12-31 | 13.0 |
| B | 2010-01-01/2010-12-31 | 4.0 |
| B | 2011-01-01/2011-12-31 | -4.0 |
| B | 2012-01-01/2012-12-31 | -4.0 |
| B | 2013-01-01/2013-12-31 | 2.0 |
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| A | 2010-01-01/2010-12-31 | 2.0 |
| A | 2011-01-01/2011-12-31 | 5.0 |
| A | 2012-01-01/2012-12-31 | -3.0 |
| A | 2013-01-01/2013-12-31 | 9.0 |
| B | 2010-01-01/2010-12-31 | 4.0 |
| B | 2011-01-01/2011-12-31 | -8.0 |
| B | 2012-01-01/2012-12-31 | 0.0 |
| B | 2013-01-01/2013-12-31 | 6.0 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗