def get_schema_from_pandas()

in webinars/snowflake_2021-09/finspace.py [0:0]


    def get_schema_from_pandas(df: pd.DataFrame):
        """
        Returns the FinSpace schema columns from the given pandas dataframe.

        :param df: pandas dataframe to interrogate for the schema

        :return: FinSpace column schema list
        """

        # for translation to FinSpace's schema
        # 'STRING'|'CHAR'|'INTEGER'|'TINYINT'|'SMALLINT'|'BIGINT'|'FLOAT'|'DOUBLE'|'DATE'|'DATETIME'|'BOOLEAN'|'BINARY'
        DoubleType = "DOUBLE"
        FloatType = "FLOAT"
        DateType = "DATE"
        StringType = "STRING"
        IntegerType = "INTEGER"
        LongType = "BIGINT"
        BooleanType = "BOOLEAN"
        TimestampType = "DATETIME"

        hab_columns = []

        for name in dict(df.dtypes):
            p_type = df.dtypes[name]

            switcher = {
                "float64": DoubleType,
                "int64": IntegerType,
                "datetime64[ns, UTC]": TimestampType,
                "datetime64[ns]": DateType
            }

            habType = switcher.get(str(p_type), StringType)

            hab_columns.append({
                "dataType": habType,
                "name": name,
                "description": ""
            })

        return (hab_columns)