search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas | to_numeric method

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas' to_numeric(~) method converts the input to a numerical type. By default, either int64 or float64 will be used.

Parameters

1. arglink | array-like

The input array, which could be a scalar, list, NumPy array or Series.

2. errorslink | string | optional

How to deal with values that cannot be parsed as a numeric:

Value

Description

"raise"

Raise an error.

"coerce"

Convert into a NaN.

"ignore"

Leave the value as is.

By default, errors="raise".

3. downcastlink | string | optional

Whether or not to perform convert numerics into the smallest numeric type (e.g. int64 to int8):

Value

Description

"integer"

Convert type to np.int8.

"signed"

Convert type to np.int8. Same as above.

"unsigned"

Convert type to np.uint8.

"float"

Convert type to np.float32.

None

Do not perform any downcasting.

Note that downcasting is performed after the main numeric conversion, and so if there are parsing issues during downcasting, then an error will be raised regardless of what you specified for errors.

By default, downcast=None.

Return Value

If arg is a Series, then a new Series is returned. Otherwise, a new Numpy array is returned.

Examples

Basic usage

To convert the type of all values in a Series to numeric type:

s = pd.Series(["1.","2.0",3])
pd.to_numeric(s)
0 1.0
1 2.0
2 3.0
dtype: float64

Note that the source Series s is left intact and a new Series is returned here.

Error handling

By default, errors="raise", which means that when a problem occurs while converting to numeric type, an error will be thrown:

s = pd.Series(["2", "2.3.4"])
pd.to_numeric(s)
ValueError: Unable to parse string "2.3.4" at position 1

Instead of throwing an error, we can convert invalid values into NaN, by specifying errors="coerce" like so:

s = pd.Series(["2", "2.3.4"])
pd.to_numeric(s, errors="coerce")
0 2.0
1 NaN
dtype: float64

We could also leave invalid values intact by using errors="ignore":

s = pd.Series(["2", "2.3.4"])
pd.to_numeric(s, errors="ignore")
0 2
1 2.3.4
dtype: object

Notice how the dtype of the Series is object. This is because a Series that contains even one non-numeric type ("2.3.4" in this case) must be upcast to a more general type, that is, object.

Downcasting

By default, numerics are converted to either int64 or float64:

s = pd.Series(["1.", "2.0", 3])
pd.to_numeric(s)
0 1.0
1 2.0
2 3.0
dtype: float64

Here, float64 is used since "2.0" is converted to a float instead of int under the hood.

We can convert this to float32 instead by passing in downcast="float" like so:

s = pd.Series(["1.", "2.0", 3])
pd.to_numeric(s, downcast="float")
0 1.0
1 2.0
2 3.0
dtype: float32

In this case, since 2.0 can be represented as an int as well, we can also pass downcast="integer" to convert the values into type int8:

s = pd.Series(["1.", "2.0", 3])
pd.to_numeric(s, downcast="integer")
0 1
1 2
2 3
dtype: int8
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!