Type Casting: cast() | Teradata Python Package - Type Casting: cast() - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-02-17
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

A DataFrame column (Column Expression) can be cast to a specified teradatasqlalchemy type using the cast method, and can be used with the DataFrame methods assign and filter.

Example Prerequisites

>>> 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

Example: Use assign to create a new DataFrame with a new column, casted to VARCHAR(5) type

Use assign() to create a new DataFrame with a new column 'char_id', which is the 'id' column (of type INTEGER) cast to VARCHAR(5) (object of teradatasqlalchemy type corresponding to SQL Type VARCHAR(5)).

>>> 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

Example: Use assign to create a new DataFrame with a new column, casted to VARCHAR type

Use assign() to create a new DataFrame with a new column 'char_id', which is the 'id' column (of type INTEGER) cast to VARCHAR (teradatasqlalchemy type corresponding to SQL Type VARCHAR).

>>> 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

Example: Filter data with match on a column cast to another type

>>> df[df.id.cast(VARCHAR(5)) == '1']
   masters   gpa     stats programming  admitted
id
1      yes  3.95  Beginner    Beginner         0

>>> df[df.id.cast(VARCHAR) == '1']
   masters   gpa     stats programming  admitted
id
1      yes  3.95  Beginner    Beginner         0