HOWTO · Pandas

How to Change Data Type of Columns in Pandas

Choose between parsing values with to_numeric(), casting selected columns with astype(), and inferring object-column types with infer_objects().

On this page

To change a Pandas column’s data type, choose to_numeric() to parse values as numbers, astype() to request a specific dtype, or infer_objects() to let Pandas infer a more specific type for object columns. These methods handle different inputs: parsing interprets numeric strings, casting applies the dtype you specify, and inference only improves types that can be inferred from the existing objects. The examples show how to choose an approach and inspect the resulting values and dtypes.

Choose the right way to change a column dtype

Use pd.to_numeric() when a Series or column contains values such as numeric strings that should become numbers. It can also turn values that cannot be parsed into NaN when you choose errors="coerce". Use astype() when you already know the desired dtype and the values can be represented by it; you can apply it to selected columns. Use infer_objects() when a DataFrame has columns with object dtype and you want Pandas to infer a more specific dtype from their current values. It is soft inference, not a general parser for arbitrary text.

Convert values to numbers with pd.to_numeric()

to_numeric() accepts a Series or array-like input and parses values as numeric data. By default, a value that cannot be parsed causes conversion to fail. Choose errors="coerce" when you want such values represented as NaN; account for those missing values in later calculations or exports.

Run this example:

import pandas as pd

values = pd.Series(["12", "4.7", "3"])
numbers = pd.to_numeric(values)
print(numbers)

To keep processing when an input value is not numeric, use coercion explicitly:

import pandas as pd

values = pd.Series(["12", "not available", "4.7"])
numbers = pd.to_numeric(values, errors="coerce")
print(numbers)

Coercion represents the unparseable entry as NaN, and the result generally has a floating-point dtype when it contains both numbers and NaN. This changes the data: do not treat coercion as a way to preserve the original text.

You can request a smaller numeric dtype with downcast. Pandas selects a smaller compatible type when the values fit; for example, downcast="integer" asks for the smallest suitable signed integer type:

import pandas as pd

values = pd.Series([-3, 1, -5])
smaller = pd.to_numeric(values, downcast="integer")
print(smaller)
print(smaller.dtype)

Downcasting affects storage dtype, not the values’ meaning. The selected type depends on the values and their range, so check the resulting dtype instead of assuming a particular integer width.

Cast selected columns with astype()

Use astype() when the target dtype is known and compatible with the column values. It can convert a Series or a DataFrame, and a dtype mapping lets you cast only named columns while leaving the others alone. For example, the second and third columns below are explicitly converted to floating-point values:

import pandas as pd

data = [["x", "1.23", "14.2"], ["y", "20", "0.11"], ["z", "3", "10"]]
df = pd.DataFrame(data, columns=["first", "second", "third"])
df = df.astype({"second": float, "third": float})
print(df)
print(df.dtypes)

Unlike to_numeric(), astype() does not offer errors="coerce" for parsing malformed numeric text. Make sure the selected values can be converted to the requested dtype; otherwise conversion can fail. Casting fractional numeric values to an integer dtype can discard the fractional part, so use a floating-point dtype when you need to retain those fractions.

Infer types for object columns with infer_objects()

infer_objects() attempts soft conversion of columns with object dtype, using the values already present to infer a more specific type where possible. It does not parse arbitrary strings such as "12" into numbers; use to_numeric() for that task. The method returns a DataFrame, so assign the result if you want subsequent operations to use the inferred dtypes:

import pandas as pd

df = pd.DataFrame({"a": [3, 12, 5], "b": [3.0, 2.6, 1.1]}, dtype="object")
print(df.dtypes)
inferred = df.infer_objects()
print(inferred.dtypes)

The dtype of a column after inference depends on its values. A numeric-looking display alone does not prove that a column has a numeric dtype; inspect the dtype information directly.

Check the resulting dtypes and conversion behavior

Use Series.dtype for one Series and DataFrame.dtypes to review every column. For a selected-column cast, check both the columns you changed and the columns you left untouched. For numeric parsing, check whether errors="coerce" introduced NaN, especially before aggregating or saving the data. For downcasting, verify the chosen width and confirm it can represent the full range of your values.

The right method follows the data and the desired result: parse numeric text with to_numeric(), explicitly cast compatible values with astype(), and use infer_objects() to improve types already stored as Python objects. For detailed API behavior, see the Pandas documentation for to_numeric() and astype().