Skip to content

ColumnType

Bases: ABC

Abstract base class for column types.

Source code in src/safeds/data/tabular/typing/_column_type.py
class ColumnType(ABC):
    """Abstract base class for column types."""

    _is_nullable: bool  # This line is just here so the linter doesn't throw an error

    @abstractmethod
    def __init__(self, is_nullable: bool = False) -> None:
        """
        Abstract initializer for ColumnType.

        Parameters
        ----------
        is_nullable
            Whether the columntype is nullable.
        """

    @staticmethod
    def _data_type(data: pd.Series) -> ColumnType:
        """
        Return the column type for a given `Series` from `pandas`.

        Parameters
        ----------
        data : pd.Series
            The data to be checked.

        Returns
        -------
        column_type : ColumnType
            The ColumnType.

        Raises
        ------
        NotImplementedError
            If the given data type is not supported.
        """
        import numpy as np

        def column_type_of_type(cell_type: Any) -> ColumnType:
            if cell_type in (int, np.int64, np.int32):
                return Integer(is_nullable)
            if cell_type in (float, np.float64, np.float32):
                return RealNumber(is_nullable)
            if cell_type == bool:
                return Boolean(is_nullable)
            if cell_type == str:
                return String(is_nullable)
            if cell_type is NoneType:
                return Nothing()
            else:
                message = f"Unsupported numpy data type '{cell_type}'."
                raise NotImplementedError(message)

        result: ColumnType = Nothing()
        is_nullable = False
        for cell in data:
            if result == Nothing():
                result = column_type_of_type(type(cell))
                if type(cell) is NoneType:
                    is_nullable = True
                    result._is_nullable = is_nullable
            if result != column_type_of_type(type(cell)):
                if type(cell) is NoneType:
                    is_nullable = True
                    result._is_nullable = is_nullable
                elif (isinstance(result, Integer) and isinstance(column_type_of_type(type(cell)), RealNumber)) or (
                    isinstance(result, RealNumber) and isinstance(column_type_of_type(type(cell)), Integer)
                ):
                    result = RealNumber(is_nullable)
                else:
                    result = Anything(is_nullable)
            if isinstance(cell, float) and np.isnan(cell):
                is_nullable = True
                result._is_nullable = is_nullable

        if isinstance(result, RealNumber) and all(
            data.apply(lambda c: bool(isinstance(c, float) and np.isnan(c) or c == float(int(c)))),
        ):
            result = Integer(is_nullable)

        return result

    @abstractmethod
    def is_nullable(self) -> bool:
        """
        Return whether the given column type is nullable.

        Returns
        -------
        is_nullable : bool
            True if the column is nullable.
        """

    @abstractmethod
    def is_numeric(self) -> bool:
        """
        Return whether the given column type is numeric.

        Returns
        -------
        is_numeric : bool
            True if the column is numeric.
        """

__init__(is_nullable=False) abstractmethod

Abstract initializer for ColumnType.

Parameters:

Name Type Description Default
is_nullable bool

Whether the columntype is nullable.

False
Source code in src/safeds/data/tabular/typing/_column_type.py
@abstractmethod
def __init__(self, is_nullable: bool = False) -> None:
    """
    Abstract initializer for ColumnType.

    Parameters
    ----------
    is_nullable
        Whether the columntype is nullable.
    """

is_nullable() abstractmethod

Return whether the given column type is nullable.

Returns:

Name Type Description
is_nullable bool

True if the column is nullable.

Source code in src/safeds/data/tabular/typing/_column_type.py
@abstractmethod
def is_nullable(self) -> bool:
    """
    Return whether the given column type is nullable.

    Returns
    -------
    is_nullable : bool
        True if the column is nullable.
    """

is_numeric() abstractmethod

Return whether the given column type is numeric.

Returns:

Name Type Description
is_numeric bool

True if the column is numeric.

Source code in src/safeds/data/tabular/typing/_column_type.py
@abstractmethod
def is_numeric(self) -> bool:
    """
    Return whether the given column type is numeric.

    Returns
    -------
    is_numeric : bool
        True if the column is numeric.
    """