atoti.OriginScope#
- final class atoti.OriginScope#
Scope performing an aggregation at the given origin.
The input of the aggregation function will be evaluated at the given
levels
and the aggregation function will be applied “above” these intermediate aggregates.Example
Using this scope with
atoti.agg.mean()
to average quantities summed by month:>>> df = pd.DataFrame( ... columns=["Year", "Month", "Day", "Quantity"], ... data=[ ... (2019, 7, 1, 15), ... (2019, 7, 2, 20), ... (2019, 7, 3, 30), ... (2019, 6, 1, 25), ... (2019, 6, 2, 15), ... (2018, 7, 1, 5), ... (2018, 7, 2, 10), ... (2018, 6, 1, 15), ... (2018, 6, 2, 5), ... ], ... ) >>> table = session.read_pandas( ... df, ... default_values={"Year": 0, "Month": 0, "Day": 0}, ... table_name="Origin", ... ) >>> cube = session.create_cube(table, mode="manual") >>> h, l, m = cube.hierarchies, cube.levels, cube.measures >>> h["Date"] = [table["Year"], table["Month"], table["Day"]] >>> m["Quantity.SUM"] = tt.agg.sum(table["Quantity"]) >>> m["Average of monthly quantities"] = tt.agg.mean( ... m["Quantity.SUM"], scope=tt.OriginScope({l["Month"]}) ... )
Average of monthly quantities will evaluate Quantity.SUM for each Month and average these values “above” this level:
>>> cube.query( ... m["Quantity.SUM"], ... m["Average of monthly quantities"], ... levels=[l["Day"]], ... include_totals=True, ... ) Quantity.SUM Average of monthly quantities Year Month Day Total 140 35.00 2018 35 17.50 6 20 20.00 1 15 15.00 2 5 5.00 7 15 15.00 1 5 5.00 2 10 10.00 2019 105 52.50 6 40 40.00 1 25 25.00 2 15 15.00 7 65 65.00 1 15 15.00 2 20 20.00 3 30 30.00
The aggregation function can be changed again to compute the max of these averages:
>>> m["Max average of monthly quantities"] = tt.agg.max( ... m["Average of monthly quantities"], ... scope=tt.OriginScope({l["Year"]}), ... ) >>> cube.query( ... m["Average of monthly quantities"], ... m["Max average of monthly quantities"], ... levels=[l["Year"]], ... include_totals=True, ... ) Average of monthly quantities Max average of monthly quantities Year Total 35.00 52.50 2018 17.50 17.50 2019 52.50 52.50