Teradata Package for Python Function Reference on VantageCloud Lake - to_numeric - 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
- 20.00.00.03
- Published
- December 2024
- ft:locale
- en-US
- ft:lastEdition
- 2024-12-19
- dita:id
- TeradataPython_FxRef_Lake_2000
- Product Category
- Teradata Vantage
- teradataml.dataframe.sql_functions.to_numeric = to_numeric(arg, **kw)
- Convert a string-like representation of a number to a Numeric type.
PARAMETERS:
arg: DataFrame column
kw: optional keyword arguments
format_: string. Specifies the format of a string-like number to convert to numeric
nls: dict where 'param' and 'value' are keys:
- param specifies one of the following string values:
-'CURRENCY', 'NUMERIC_CHARACTERS', 'DUAL_CURRENCY', 'ISO_CURRENCY'
- value: specifies characters that are returned by number format elements.
See References for more information
REFERENCES:
Chapter 14: Data Type Conversion Functions
Teradata® Database SQL Functions, Operators, Expressions, and
Predicates, Release 16.20
RETURNS:
A DataFrame column of numeric type
NOTES:
- If the arg column input is a numeric type, it is returned as is
- Nulls may be introduced in the result if the parsing fails
- You may need to strip() columns that have leading or trailing spaces
in order for to_numeric to parse correctly
EXAMPLES:
>>> df = DataFrame('numeric_strings')
hex decimal commas numbers
0 19FF 00.77 08,8 1
1 abcd 0.77 0,88 1
2 ABCDEFABCD 0.7.7 ,088 999
3 2018 .077 088, 0
>>> df.dtypes
hex str
decimal str
commas str
numbers str
# converting string numbers to numeric
>>> df.assign(drop_columns = True,
numbers = df.numbers,
numeric = to_numeric(df.numbers))
numbers numeric
0 1 1
1 1 1
2 999 999
3 0 0
# converting decimal-like strings to numeric
# Note that strings not following the format return None
>>> df.assign(drop_columns = True,
decimal = df.decimal,
numeric_dec = to_numeric(df.decimal))
decimal numeric_dec
0 00.77 .77
1 0.77 .77
2 0.7.7 None
3 .077 .077
# converting comma (group separated) strings to numeric
# Note that strings not following the format return None
>>> df.assign(drop_columns = True,
commas = df.commas,
numeric_commas = to_numeric(df.commas, format_ = '9G99'))
commas numeric_commas
0 08,8 None
1 0,88 88
2 ,088 None
3 088, None
# converting hex strings to numeric
>>> df.assign(drop_columns = True,
hex = df.hex,
numeric_hex = to_numeric(df.hex, format_ = 'XXXXXXXXXX'))
hex numeric_hex
0 19FF 6655
1 abcd 43981
2 ABCDEFABCD 737894443981
3 2018 8216
# converting literals to numeric
>>> df.assign(drop_columns = True,
a = to_numeric('123,456',format_ = '999,999'),
b = to_numeric('1,333.555', format_ = '9,999D999'),
c = to_numeric('2,333,2',format_ = '9G999G9'),
d = to_numeric('3E20'),
e = to_numeric('$41.99', format_ = 'L99.99'),
f = to_numeric('$.12', format_ = 'L.99'),
g = to_numeric('dollar123,456.00',
format_ = 'L999G999D99',
nls = {'param': 'currency', 'value': 'dollar'})).head(1)
a b c d e f g
0 123456 1333.555 23332 300000000000000000000 41.99 .12 123456
# For more information on format elements and parameters, see the Reference.