Coverage for src/ipyvizzu/data/converters/df/converter.py: 100%
29 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-02-26 10:12 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-02-26 10:12 +0000
1"""
2This module provides the `DataFrameConverter` abstract class.
3"""
5from abc import abstractmethod
6from typing import Dict, List, Optional
7import warnings
9from ipyvizzu.data.converters.converter import ToSeriesListConverter
10from ipyvizzu.data.converters.df.type_alias import DataFrame
11from ipyvizzu.data.type_alias import (
12 DimensionValue,
13 MeasureValue,
14 Series,
15)
18class DataFrameConverter(ToSeriesListConverter):
19 """
20 Converts data frame into a list of dictionaries representing series.
21 Each dictionary contains information about the series `name`, `values` and `type`.
22 """
24 # pylint: disable=too-few-public-methods
26 def __init__(
27 self,
28 default_measure_value: MeasureValue,
29 default_dimension_value: DimensionValue,
30 max_rows: int,
31 units: Optional[Dict[str, str]] = None,
32 ) -> None:
33 super().__init__(default_measure_value, default_dimension_value)
34 self._max_rows = max_rows
35 self._units = units or {}
37 def get_series_list(self) -> List[Series]:
38 """
39 Convert the `DataFrame` columns to a list of dictionaries representing series.
41 Returns:
42 A list of dictionaries representing series,
43 where each dictionary has `name`, `values` and `type` keys.
44 """
46 series_list = []
47 for name in self._get_columns():
48 series_list.append(self._get_series_from_column(name))
49 return series_list
51 def _get_series_from_column(self, column_name: str) -> Series:
52 values, infer_type = self._convert_to_series_values_and_type(column_name)
53 unit = self._units.get(column_name, None)
54 return self._convert_to_series(column_name, values, infer_type, unit)
56 def _is_max_rows_exceeded(self, row_number: int) -> bool:
57 if row_number > self._max_rows:
58 warnings.warn(
59 "The number of rows of the dataframe exceeds the set `max_rows`, "
60 f"the dataframe is randomly sampled to the set value ({self._max_rows}).",
61 UserWarning,
62 stacklevel=2,
63 )
64 return True
65 return False
67 @abstractmethod
68 def _get_sampled_df(self, df: DataFrame) -> DataFrame:
69 """
70 Returns a sampled data frame for the maximum number of rows.
71 """
73 @abstractmethod
74 def _get_columns(self) -> List[str]:
75 """
76 Return column names of the data frame.
77 """