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
A | B | C | ||
---|---|---|---|---|
str32 | str32 | int32 | ||
0 | 1 | NA | 1 | |
1 | 1 | 2 | 2 | |
2 | 2 | 3 | 1 | |
3 | 1 | 4 | 1 | |
4 | 2 | 5 | 2 |
Convert column A from string to integer type:
df[:, as_type(f.A, int)]
A | ||
---|---|---|
int64 | ||
0 | 1 | |
1 | 1 | |
2 | 2 | |
3 | 1 | |
4 | 2 |
The exact dtype can be specified:
df[:, as_type(f.A, dt.Type.int32)]
A | ||
---|---|---|
int32 | ||
0 | 1 | |
1 | 1 | |
2 | 2 | |
3 | 1 | |
4 | 2 |
Convert multiple columns to different types:
df[:, [as_type(f.A, int), as_type(f.C, dt.str32)]]
A | C | ||
---|---|---|---|
int64 | str32 | ||
0 | 1 | 1 | |
1 | 1 | 2 | |
2 | 2 | 1 | |
3 | 1 | 1 | |
4 | 2 | 2 |
The content on this page is licensed under the Creative Commons Attribution 4.0 License
(CC BY 4.0) ,
and code samples are licensed under the MIT License.