Skip to content

CubedPandas Context Class

Bases: SupportsFloat

A context represents a multi-dimensional data context or area from within a cube. Context objects can be used to navigate and access the data of a cube and thereby the underlying dataframe.

Cells behave like Python floats and return a numeric aggregation of the underlying data. They are intended to be used in mathematical operations.

Samples

cdf = cubed(df) value = cdf.A + cdf.B / 2 200 cdf.A *= 2

function: ContextFunction property

Returns:

  • ContextFunction

    The aggregation function that will be applied to the current context.

value property writable

Returns:

  • The sum value of the current context from the underlying cube.

numeric_value: float property

Returns:

  • float

    The numerical value of the current context from the underlying cube.

cube: Cube property

Returns:

  • Cube

    The Cube object the Context belongs to.

dimension: Dimension property

Returns:

  • Dimension

    The Cube object the Context belongs to.

parent: Context property

Returns:

  • Context

    The parent Context of the current Context. If the current Context is the root Context of the cube,

  • Context

    then the parent Context will be None.

df: pd.DataFrame property

Returns: Returns a new Pandas dataframe with all column of the underlying dataframe of the Cube, but only with the rows that are represented by the current context.

The returned dataframe is always a copy of the original dataframe, even if the context is not filtering any rows from the underlying dataframe. The returned dataframe can be used for further processing outside the cube.

address: any property

Returns:

  • any

    The partial address of the context, as defined by the user

  • any

    This does not include the addresses defined by predecessor

  • any

    cells down to the cube.

cube_address: str property

Returns:

  • str

    The full address of the context, including all predecessor

  • str

    cells down to the cube.

measure: Measure property writable

Returns:

  • Measure

    The Measure object the Context is currently referring to.

  • Measure

    The measure refers to a column in the underlying dataframe

  • Measure

    that is used to calculate the value of the context.

row_mask: np.ndarray | None property

Returns:

  • ndarray | None

    The row mask of the context. The row mask is represented by a Numpy ndarray

  • ndarray | None

    of the indexes of the rows represented by the current context. The row mask can be used

  • ndarray | None

    for subsequent processing of the underlying dataframe outside the cube.

member_mask: np.ndarray | None property

Returns:

  • ndarray | None

    The member mask of the context. If the context refers to a member or a set of members from a dimension.

  • ndarray | None

    then a Numpy ndarray containing the indexes of the rows representing the members is returned.

  • ndarray | None

    None is returned otherwise.

  • ndarray | None

    The row mask can be used for subsequent processing of the underlying dataframe outside the cube.

row_mask_inverse: np.ndarray property

Returns:

  • ndarray

    The inverted row mask of the context. The inverted row mask is represented by a Numpy ndarray

  • ndarray

    of the indexes of the rows NOT represented by the current context. The inverted row mask

  • ndarray

    can be used for subsequent processing of the underlying dataframe outside the cube.

__init__(cube, address, parent=None, row_mask=None, member_mask=None, measure=None, dimension=None, function=ContextFunction.SUM, resolve=True, filtered=False, dynamic_attribute=False)

Initializes a new Context object. For internal use only. Raises: ValueError: If the address is invalid and does not refer to a dimension, member or measure of the cube.

set_value(value, allocation_function=ContextAllocation.DISTRIBUTE)

Writes a value to the current context of the cube down to the underlying dataframe. The allocation method can be chosen.

Parameters:

  • value

    The value to be written to the cube.

  • allocation_function (ContextAllocation, default: DISTRIBUTE ) –

    The allocation function to be used for writing the value to the cube.

Returns: The new value of the current context from the underlying cube.

top(n)

Returns the top n members of the current context. Args: n: The number of top members to be returned. Returns: A list of the top n members of the current context.

bottom(n)

Returns the bottom n members of the current context. Args: n: The number of bottom members to be returned. Returns: A list of the bottom n members of the current context.

__getattr__(name)

Dynamically resolves member from the cube and predecessor cells.

__getitem__(address)

Returns a nested context of the cube and for a given address. Subsequent nested cells can bee seen as subsequent filters upon the underlying dataframe.

Parameters:

  • address

    A valid cube address. Please refer the documentation for further details.

Returns:

  • A Context object that represents the cube data related to the address

  • and all predecessor cells down to the cube.

Raises:

  • ValueError

    If the address is not valid or can not be resolved.

__setitem__(address, value)

Sets a value for a given address in the cube. Args: address: A valid cube address. Please refer the documentation for further details. value: The value to be set for the data represented by the address. Raises: PermissionError: If write back is attempted on a read-only Cube.

__delitem__(address)

Deletes the records represented by the given address from the underlying dataframe of the cube. Args: address: A valid cube address. Please refer the documentation for further details. Raises: PermissionError: If write back is attempted on a read-only Cube.

slice(rows=None, columns=None, config=None)

Returns a new slice for the context. A slice represents a table-alike view to data in the cube. Typically, a slice has rows, columns and filters, comparable to an Excel PivotTable. Useful for printing in Jupyter, visual data analysis and reporting purposes. Slices can be easily 'navigated' by setting and changing rows, columns and filters.

Please refer to the documentation of the Slice class for further details.

Parameters:

  • rows

    The rows of the slice. Can be one or more dimensions with or without a member definition, or no dimension.

  • columns

    The columns of the slice. Can be one or more dimensions with or without a member definition, or no dimension.

  • filters

    The filters of the slice. Can be one or more dimensions with or without a member definition, or no dimension.

  • config

    (optional) A slice configuration as a dictionary, a json string or a path to an existing file containing the configuration. Slice configurations can be used to define a more complex layout. Please refer to the documentation of the Slice class for further details.

Samples

cdf = cubed(df) cdf.slice(rows="product", columns="region", filters={"year": 2020})


year: 2000

| | (all) | North | South |

| (all) | 550 | 300 | 250 | | Apple | 200 | 100 | 100 | | Banana | 350 | 200 | 150 |

filter(filter)

Filters the current context by a given filter expression, criteria or callable function. Args: filter: The filter expression, criteria or callable function to be used for filtering the context. Returns: A new context with the filtered data.