Teradata Package for Python Function Reference on VantageCloud Lake | 17.20 - cast - Teradata Package for Python - Look here for syntax, methods and examples for the functions included in the Teradata Package for Python.

Teradata® Package for Python Function Reference on VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Package for Python
Release Number
17.20
Publication Date
November 2022
Content Type
Programming Reference
Publication ID
B700-4500-112K
ft:locale
en-US
ft:lastEdition
2024-02-21
ft:sourceId
FTML
dita:id
TeradataPython_FxRef_Lake_1720
Product Category
Teradata Vantage
teradataml.dataframe.sql.DataFrameColumn.cast = cast(self, type_=None)
DESCRIPTION:
    Apply the CAST SQL function to the column with the type specified.
 
    NOTE: This method can currently be used only with 'filter' and
          'assign' methods of teradataml DataFrame.
 
PARAMETERS:
    type_:
        Required Argument.
        Specifies a teradatasqlalchemy type or an object of a teradatasqlalchemy type
        that the column needs to be cast to.
        Default value: None
        Types: teradatasqlalchemy type or object of teradatasqlalchemy type
 
EXAMPLES:
    >>> load_example_data("dataframe","admissions_train")
    >>> df = DataFrame('admissions_train')
    >>> df
       masters   gpa     stats programming  admitted
    id
    13      no  4.00  Advanced      Novice         1
    26     yes  3.57  Advanced    Advanced         1
    5       no  3.44    Novice      Novice         0
    19     yes  1.98  Advanced    Advanced         0
    15     yes  4.00  Advanced    Advanced         1
    40     yes  3.95    Novice    Beginner         0
    7      yes  2.33    Novice      Novice         1
    22     yes  3.46    Novice    Beginner         0
    36      no  3.00  Advanced      Novice         0
    38     yes  2.65  Advanced    Beginner         1
    >>> df.dtypes
    id               int
    masters          str
    gpa            float
    stats            str
    programming      str
    admitted         int
 
    >>> # Let's try creating a new DataFrame casting 'id' column (of type INTEGER) to VARCHAR(5),
    >>> # an object of a teradatasqlalchemy type.
    >>> from teradatasqlalchemy import VARCHAR
    >>> new_df = df.assign(char_id = df.id.cast(type_=VARCHAR(5)))
    >>> new_df
       masters   gpa     stats programming  admitted char_id
    id
    5       no  3.44    Novice      Novice         0       5
    34     yes  3.85  Advanced    Beginner         0      34
    13      no  4.00  Advanced      Novice         1      13
    40     yes  3.95    Novice    Beginner         0      40
    22     yes  3.46    Novice    Beginner         0      22
    19     yes  1.98  Advanced    Advanced         0      19
    36      no  3.00  Advanced      Novice         0      36
    15     yes  4.00  Advanced    Advanced         1      15
    7      yes  2.33    Novice      Novice         1       7
    17      no  3.83  Advanced    Advanced         1      17
    >>> new_df.dtypes
    id               int
    masters          str
    gpa            float
    stats            str
    programming      str
    admitted         int
    char_id          str
 
    >>> # Now let's try creating a new DataFrame casting 'id' column (of type INTEGER) to VARCHAR,
    >>> # a teradatasqlalchemy type.
    >>> new_df_2 = df.assign(char_id = df.id.cast(type_=VARCHAR))
    >>> new_df_2
       masters   gpa     stats programming  admitted char_id
    id
    5       no  3.44    Novice      Novice         0       5
    34     yes  3.85  Advanced    Beginner         0      34
    13      no  4.00  Advanced      Novice         1      13
    40     yes  3.95    Novice    Beginner         0      40
    22     yes  3.46    Novice    Beginner         0      22
    19     yes  1.98  Advanced    Advanced         0      19
    36      no  3.00  Advanced      Novice         0      36
    15     yes  4.00  Advanced    Advanced         1      15
    7      yes  2.33    Novice      Novice         1       7
    17      no  3.83  Advanced    Advanced         1      17
    >>> new_df_2.dtypes
    id               int
    masters          str
    gpa            float
    stats            str
    programming      str
    admitted         int
    char_id          str
 
    >>> # Let's try filtering some data with a match on a column cast to another type,
    >>> # an object of a teradatasqlalchemy type.
    >>> df[df.id.cast(VARCHAR(5)) == '1']
       masters   gpa     stats programming  admitted
    id
    1      yes  3.95  Beginner    Beginner         0
 
    >>> # Now let's try the same, this time using a teradatasqlalchemy type.
    >>> df[df.id.cast(VARCHAR) == '1']
       masters   gpa     stats programming  admitted
    id
    1      yes  3.95  Beginner    Beginner         0
 
RETURNS:
    _SQLColumnExpression
 
RAISES:
    TeradataMlException