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

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

atoti.where().

Return type

MeasureDescription