datatable.as_type()

Added in version 1.0

Convert columns cols into the prescribed stype.

This function does not modify the data in the original column. Instead it returns a new column which converts the values into the new type on the fly.

Parameters

cols
FExpr

Single or multiple columns that need to be converted.

new_type
Type | stype

Target type.

return
FExpr

The output will have the same number of rows and columns as the input; column names will be preserved too.

Examples

from datatable import dt, f, as_type df = dt.Frame({'A': ['1', '1', '2', '1', '2'], 'B': [None, '2', '3', '4', '5'], 'C': [1, 2, 1, 1, 2]}) df
ABC
str32str32int32
01NA1
1122
2231
3141
4252

Convert column A from string to integer type:

df[:, as_type(f.A, int)]
A
int64
01
11
22
31
42

The exact dtype can be specified:

df[:, as_type(f.A, dt.Type.int32)]
A
int32
01
11
22
31
42

Convert multiple columns to different types:

df[:, [as_type(f.A, int), as_type(f.C, dt.str32)]]
AC
int64str32
011
112
221
311
422