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

Teradata® Package for Python User Guide

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for Python
Release Number
20.00
Published
March 2024
Language
English (United States)
Last Update
2024-04-09
dita:mapPath
nvi1706202040305.ditamap
dita:ditavalPath
plt1683835213376.ditaval
dita:id
rkb1531260709148
Product Category
Teradata Vantage

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

Required Argument:
  • type_: Specifies a teradatasqlalchemy type or an object of a teradatasqlalchemy type that the column needs to be cast to.

    Default value is None.

Example Setup

>>> 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 1: 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 2: 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 3: 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