Skip to content

table

table

TableEntry pydantic-model

Bases: DataProduct

Defines an entry to be collected into a table.

Collected table entries will be printed in three forms when possible: melted, pivot (when possible), and stats (on pivot columns, when possible).

Attributes:

Name Type Description
tags Tags

Tags used to sort data products

row float | str

Row Label

col float | str

Column Label

value float | str

Value

unit str | None

Units for value

metadata dict[str, str]

A dictionary of metadata to be used as a tool tip for mousover in grafana

Show JSON schema:
{
  "additionalProperties": false,
  "description": "Defines an entry to be collected into a table.\n\nCollected table entries will be printed in three forms when possible: melted, pivot (when possible), and stats (on pivot columns, when possible).\n\nAttributes:\n    tags (Tags): Tags used to sort data products\n    row (float | str): Row Label\n    col (float | str): Column Label\n    value (float | str): Value\n    unit (str | None): Units for value\n    metadata (dict[str, str]): A dictionary of metadata to be used as a tool tip for mousover in grafana",
  "properties": {
    "tags": {
      "items": {
        "anyOf": []
      },
      "title": "Tags",
      "type": "array"
    },
    "metadata": {
      "additionalProperties": {
        "type": "string"
      },
      "default": {},
      "title": "Metadata",
      "type": "object"
    },
    "row": {
      "anyOf": [
        {
          "type": "number"
        },
        {
          "type": "string"
        }
      ],
      "title": "Row"
    },
    "col": {
      "anyOf": [
        {
          "type": "number"
        },
        {
          "type": "string"
        }
      ],
      "title": "Col"
    },
    "value": {
      "anyOf": [
        {
          "type": "number"
        },
        {
          "type": "string"
        },
        {
          "type": "boolean"
        }
      ],
      "title": "Value"
    },
    "unit": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Unit"
    }
  },
  "required": [
    "tags",
    "row",
    "col",
    "value"
  ],
  "title": "TableEntry",
  "type": "object"
}

Config:

  • extra: 'forbid'

Fields:

get_entry_dict

get_entry_dict()

Returns a dictionary of entries to be used in creating a table.

Returns:

Type Description
dict[str, str | float]

Dictionary of entries to be used in creating a melted DataFrame

Source code in src/trendify/api/formats/table.py
def get_entry_dict(self):
    """
    Returns a dictionary of entries to be used in creating a table.

    Returns:
        (dict[str, str | float]): Dictionary of entries to be used in creating a melted [DataFrame][pandas.DataFrame]
    """
    return {
        "row": self.row,
        "col": self.col,
        "value": self.value,
        "unit": self.unit,
    }

load_and_pivot classmethod

load_and_pivot(path: Path)

Loads melted table from csv and pivots to wide form. csv should have columns named 'row', 'col', and 'value'.

Parameters:

Name Type Description Default
path Path

path to CSV file

required

Returns:

Type Description
DataFrame | None

Pivoted data frame or elese None if pivot operation fails.

Source code in src/trendify/api/formats/table.py
@classmethod
def load_and_pivot(cls, path: Path):
    """
    Loads melted table from csv and pivots to wide form.
    csv should have columns named `'row'`, `'col'`, and `'value'`.

    Args:
        path (Path): path to CSV file

    Returns:
        (pd.DataFrame | None): Pivoted data frame or elese `None` if pivot operation fails.
    """
    return cls.pivot_table(melted=pd.read_csv(path))

pivot_table classmethod

pivot_table(melted: DataFrame)

Attempts to pivot melted row, col, value DataFrame into a wide form DataFrame

Parameters:

Name Type Description Default
melted DataFrame

Melted data frame having columns named 'row', 'col', 'value'.

required

Returns:

Type Description
DataFrame | None

pivoted DataFrame if pivot works else None. Pivot operation fails if row or column index pairs are repeated.

Source code in src/trendify/api/formats/table.py
@classmethod
def pivot_table(cls, melted: pd.DataFrame):
    """
    Attempts to pivot melted row, col, value DataFrame into a wide form DataFrame

    Args:
        melted (pd.DataFrame): Melted data frame having columns named `'row'`, `'col'`, `'value'`.

    Returns:
        (pd.DataFrame | None): pivoted DataFrame if pivot works else `None`. Pivot operation fails if
            row or column index pairs are repeated.
    """
    try:
        result = melted.pivot(index="row", columns="col", values="value")
    except ValueError as e:
        logger.debug(traceback.format_exc())
        result = None
    return result