Ejemplo n.º 1
0
    def from_dataframe(self,
                       dataframe,
                       add_index_column: bool = False,
                       overwrite_type_hints: bool = True) -> None:
        """
        Set tabular attributes to the writer from :py:class:`pandas.DataFrame`.
        Following attributes are set by the method:

            - :py:attr:`~.headers`
            - :py:attr:`~.value_matrix`
            - :py:attr:`~.type_hints`

        Args:
            dataframe(pandas.DataFrame or |str|):
                Input pandas.DataFrame object or pickle.
            add_index_column(bool, optional):
                If |True|, add a column of ``index`` of the ``dataframe``.
                Defaults to |False|.
            overwrite_type_hints(bool):
                If |True|, Overwrite type hints with dtypes within the DataFrame.

        Example:
            :ref:`example-from-pandas-dataframe`
        """

        if typepy.String(dataframe).is_type():
            import pandas as pd

            dataframe = pd.read_pickle(dataframe)

        self.headers = list(dataframe.columns.values)

        if not self.type_hints or overwrite_type_hints:
            self.type_hints = [
                extract_typepy_from_dtype(dtype) for dtype in dataframe.dtypes
            ]

        if add_index_column:
            self.headers = [" "] + self.headers
            if self.type_hints:
                self.type_hints = [Integer] + self.type_hints
            self.value_matrix = [[index] + row for index, row in zip(
                dataframe.index.tolist(), dataframe.values.tolist())]
        else:
            self.value_matrix = dataframe.values.tolist()
Ejemplo n.º 2
0
 def test_normal(self, method, strict_level, value, expected):
     assert convert_wrapper(typepy.String(value, strict_level),
                            method) == expected
Ejemplo n.º 3
0
 def _normalize_header(self, header: str) -> str:
     return typepy.String(header).force_convert()
Ejemplo n.º 4
0
 def _validate_header(self, header: str) -> None:
     try:
         typepy.String(header).validate()
     except TypeError as e:
         raise InvalidHeaderNameError(e)
Ejemplo n.º 5
0
 def _normalize_table_name(self, table_name: str) -> str:
     return typepy.String(table_name).force_convert()
Ejemplo n.º 6
0
 def _validate_table_name(self, table_name: str) -> None:
     try:
         typepy.String(table_name).validate()
     except TypeError as e:
         raise InvalidTableNameError(e)