Loading...

verticapy.get_data_types#

verticapy.get_data_types(expr: str | None = None, column: str | None = None, table_name: str | None = None, schema: str | None = None, usecols: list | None = None) tuple | list[tuple]#

Returns customized relation columns and the respective data types. This process creates a temporary table.

If table_name is defined, the expression is ignored and the function returns the table/ view column names and data types.

Parameters#

expr: str, optional

An expression in pure SQL. If empty, the parameter ‘table_name’ must be defined.

column: str, optional

If not empty, the function returns only the data type of the input column if it is in the relation.

table_name: str, optional

Input table Name.

schema: str, optional

Table schema.

usecols: list, optional

List of columns to consider. This parameter can not be used if ‘column’ is defined.

Returns#

list of tuples

The list of the different columns and their respective type.

Examples#

Let’s import the function before proceeding.

from verticapy.sql import get_data_types

You can easily retrieve all the types of the columns in your SQL query by using this function.

get_data_types(
    "SELECT "
    "pclass, embarked, AVG(survived) "
    "FROM public.titanic "
    "GROUP BY 1, 2"
)

Out[2]: [['pclass', 'Integer'], ['embarked', 'Varchar(20)'], ['AVG', 'Float(22)']]

You can also retrieve the type of a specific column.

get_data_types(
    "SELECT "
    "pclass, embarked, AVG(survived) "
    "FROM public.titanic "
    "GROUP BY 1, 2",
    column = "pclass",
)

Out[3]: 'Integer'

Note

This function is employed to determine the column types in a SQL query. While it’s straightforward for tables and views stored in Vertica, for SQL queries, we need to store an empty table to retrieve the final types. Another approach is to directly guess the types from the cursor result.

See also

vertica_python_dtype() : Formats the input data type.