def np_type_to_df_type()

in odps/df/backends/pd/types.py [0:0]


def np_type_to_df_type(dtype, arr=None, unknown_as_string=False, name=None):
    from ..odpssql.types import odps_type_to_df_type

    if dtype in _np_to_df_types:
        return _np_to_df_types[dtype]
    if hasattr(pd, "ArrowDtype") and isinstance(dtype, pd.ArrowDtype):
        return odps_type_to_df_type(arrow_type_to_odps_type(dtype.pyarrow_dtype))
    if hasattr(pd, "StringDtype") and isinstance(dtype, pd.StringDtype):
        return types.string
    if ExtensionDtype is not None and isinstance(dtype, ExtensionDtype):
        for dt_name, df_type in _pd_ext_type_name_to_df_type.items():
            if hasattr(pd, dt_name) and isinstance(dtype, getattr(pd, dt_name)):
                return df_type

    name = ', field: ' + name if name else ''
    if arr is None or len(arr) == 0:
        if dtype == np.dtype(object) and unknown_as_string:
            return types.string
        raise TypeError('Unknown dtype: %s%s' % (dtype, name))

    for it in arr:
        if it is None or is_na_func(it):
            continue
        if isinstance(it, six.string_types):
            return types.string
        elif isinstance(it, six.integer_types):
            return types.int64
        elif isinstance(it, float):
            return types.float64
        elif isinstance(it, datetime):
            return types.datetime
        elif isinstance(it, Decimal):
            return types.decimal
        elif unknown_as_string:  # not inferred
            return types.string
        else:
            raise TypeError('Unknown dtype: %s%s' % (dtype, name))

    if unknown_as_string:
        return types.string
    raise TypeError('Unknown dtype: %s' % dtype)