atoti.function.switch module#
- atoti.switch(subject, cases, /, *, default=None)#
Return a measure equal to the value of the first case for which subject is equal to the case’s key.
cases’s values and default must either be all numerical, all boolean or all objects.
- Parameters:
subject (NonConstantMeasureConvertible) – The measure or level to compare to cases’ keys.
cases (Mapping[MeasureConvertible | None | tuple[Optional[MeasureConvertible], ...], MeasureConvertible]) – A mapping from keys to compare with subject to the values to return if the comparison is
True
.default (MeasureConvertible | None) – The measure to use when none of the cases matched.
- Return type:
MeasureDescription
Example
>>> df = pd.DataFrame( ... columns=["Id", "City", "Value"], ... data=[ ... (0, "Paris", 1.0), ... (1, "Paris", 2.0), ... (2, "London", 3.0), ... (3, "London", 4.0), ... (4, "Paris", 5.0), ... (5, "Singapore", 7.0), ... (6, "NYC", 2.0), ... ], ... ) >>> table = session.read_pandas(df, keys=["Id"], table_name="Switch example") >>> cube = session.create_cube(table) >>> l, m = cube.levels, cube.measures >>> m["Continent"] = tt.switch( ... l["City"], ... { ... ("Paris", "London"): "Europe", ... "Singapore": "Asia", ... "NYC": "North America", ... }, ... ) >>> cube.query(m["Continent"], levels=[l["City"]]) Continent City London Europe NYC North America Paris Europe Singapore Asia >>> m["Europe & Asia value"] = tt.agg.sum( ... tt.switch( ... m["Continent"], {("Europe", "Asia"): m["Value.SUM"]}, default=0.0 ... ), ... scope=tt.OriginScope(l["Id"], l["City"]), ... ) >>> cube.query(m["Europe & Asia value"], levels=[l["City"]]) Europe & Asia value City London 7.00 NYC .00 Paris 8.00 Singapore 7.00 >>> cube.query(m["Europe & Asia value"]) Europe & Asia value 0 22.00
See also