Edit on GitHub

sqlmesh.utils.pandas

 1from __future__ import annotations
 2
 3import typing as t
 4
 5import numpy as np
 6import pandas as pd
 7from sqlglot import exp
 8
 9
10PANDAS_TYPE_MAPPINGS = {
11    np.dtype("int8"): exp.DataType.build("tinyint"),
12    np.dtype("int16"): exp.DataType.build("smallint"),
13    np.dtype("int32"): exp.DataType.build("int"),
14    np.dtype("int64"): exp.DataType.build("bigint"),
15    np.dtype("float16"): exp.DataType.build("float"),
16    np.dtype("float32"): exp.DataType.build("float"),
17    np.dtype("float64"): exp.DataType.build("double"),
18    np.dtype("O"): exp.DataType.build("text"),
19    np.dtype("bool"): exp.DataType.build("boolean"),
20    np.dtype("datetime64"): exp.DataType.build("timestamp"),
21    np.dtype("datetime64[ns]"): exp.DataType.build("timestamp"),
22    np.dtype("datetime64[us]"): exp.DataType.build("timestamp"),
23    pd.Int8Dtype(): exp.DataType.build("tinyint"),
24    pd.Int16Dtype(): exp.DataType.build("smallint"),
25    pd.Int32Dtype(): exp.DataType.build("int"),
26    pd.Int64Dtype(): exp.DataType.build("bigint"),
27    pd.Float32Dtype(): exp.DataType.build("float"),
28    pd.Float64Dtype(): exp.DataType.build("double"),
29    pd.StringDtype(): exp.DataType.build("text"),  # type: ignore
30    pd.BooleanDtype(): exp.DataType.build("boolean"),
31}
32
33
34def columns_to_types_from_df(df: pd.DataFrame) -> t.Dict[str, exp.DataType]:
35    result = {}
36    for column_name, column_type in df.dtypes.items():
37        exp_type = PANDAS_TYPE_MAPPINGS.get(column_type)
38        if not exp_type:
39            raise ValueError(f"Unsupported pandas type '{column_type}'")
40        result[str(column_name)] = exp_type
41    return result
def columns_to_types_from_df( df: pandas.core.frame.DataFrame) -> Dict[str, sqlglot.expressions.DataType]:
35def columns_to_types_from_df(df: pd.DataFrame) -> t.Dict[str, exp.DataType]:
36    result = {}
37    for column_name, column_type in df.dtypes.items():
38        exp_type = PANDAS_TYPE_MAPPINGS.get(column_type)
39        if not exp_type:
40            raise ValueError(f"Unsupported pandas type '{column_type}'")
41        result[str(column_name)] = exp_type
42    return result