Skip to content

Converter

ipyvizzu.data.converters.df.converter

This module provides the DataFrameConverter abstract class.

ipyvizzu.data.converters.df.converter.DataFrameConverter

Bases: ToSeriesListConverter

Converts data frame into a list of dictionaries representing series. Each dictionary contains information about the series name, values and type.

Source code in src/ipyvizzu/data/converters/df/converter.py
class DataFrameConverter(ToSeriesListConverter):
    """
    Converts data frame into a list of dictionaries representing series.
    Each dictionary contains information about the series `name`, `values` and `type`.
    """

    # pylint: disable=too-few-public-methods

    def __init__(
        self,
        default_measure_value: MeasureValue,
        default_dimension_value: DimensionValue,
        max_rows: int,
        units: Optional[Dict[str, str]] = None,
    ) -> None:
        super().__init__(default_measure_value, default_dimension_value)
        self._max_rows = max_rows
        self._units = units or {}

    def get_series_list(self) -> List[Series]:
        """
        Convert the `DataFrame` columns to a list of dictionaries representing series.

        Returns:
            A list of dictionaries representing series,
            where each dictionary has `name`, `values` and `type` keys.
        """

        series_list = []
        for name in self._get_columns():
            series_list.append(self._get_series_from_column(name))
        return series_list

    def _get_series_from_column(self, column_name: str) -> Series:
        values, infer_type = self._convert_to_series_values_and_type(column_name)
        unit = self._units.get(column_name, None)
        return self._convert_to_series(column_name, values, infer_type, unit)

    def _is_max_rows_exceeded(self, row_number: int) -> bool:
        if row_number > self._max_rows:
            warnings.warn(
                "The number of rows of the dataframe exceeds the set `max_rows`, "
                f"the dataframe is randomly sampled to the set value ({self._max_rows}).",
                UserWarning,
                stacklevel=2,
            )
            return True
        return False

    @abstractmethod
    def _get_sampled_df(self, df: DataFrame) -> DataFrame:
        """
        Returns a sampled data frame for the maximum number of rows.
        """

    @abstractmethod
    def _get_columns(self) -> List[str]:
        """
        Return column names of the data frame.
        """

get_series_list()

Convert the DataFrame columns to a list of dictionaries representing series.

Returns:

Type Description
List[Series]

A list of dictionaries representing series,

List[Series]

where each dictionary has name, values and type keys.

Source code in src/ipyvizzu/data/converters/df/converter.py
def get_series_list(self) -> List[Series]:
    """
    Convert the `DataFrame` columns to a list of dictionaries representing series.

    Returns:
        A list of dictionaries representing series,
        where each dictionary has `name`, `values` and `type` keys.
    """

    series_list = []
    for name in self._get_columns():
        series_list.append(self._get_series_from_column(name))
    return series_list