Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.astype

DataFrame.astype casts an entire DataFrame to one dtype or applies dtypes column by column through a mapping. Invalid conversions raise by default, with errors='ignore' available to suppress them.

How it works

Call astype with one dtype to convert the whole DataFrame, or pass a column-to-dtype mapping for selective conversion. The method returns a DataFrame with the requested types; invalid conversions raise by default unless errors is set to ignore.

Syntax

DataFrame.astype(dtype, copy=<no_default>, errors='raise')

Parameters

NameTypeDescription
dtypestr, data type, Series, or Mapping of column name to data typeTarget dtype for the entire object, or a mapping of column names to individual target dtypes. Supported dtype forms include strings, numpy dtypes, pandas extension dtypes, Python types, and Series.
copybool or no_defaultWhether to copy data when performing the conversion. The current documentation signature uses a no-default sentinel.
errors{'raise', 'ignore'}, default 'raise'Controls exception handling for invalid data: raise exceptions or suppress them.

Returns

DataFrame

Key points

  • DataFrame.astype converts the DataFrame to requested data types.
  • A single dtype applies one target type across the complete DataFrame.
  • A column-to-dtype mapping enables different target types for different columns.
  • The dtype parameter supports scalar dtype specifications and column mappings.
  • The errors parameter accepts raise or ignore and defaults to raise.

Common mistakes

  • With the default errors='raise', values that cannot be converted cause an exception; errors='ignore' suppresses that exception.

Example: Cast selected columns

import pandas as pd

df = pd.DataFrame({"count": [1, 2], "score": [3, 4]})
converted = df.astype({"count": "int32", "score": "float32"})
print(converted.dtypes)

A mapping applies a separate target dtype to each named column.

When to use it

Whole-DataFrame type normalization

Normalize every column to a shared numeric or string representation before downstream processing.

Per-column conversion

Convert selected columns to different numeric widths or other target dtypes while preserving unmapped columns.

Tolerant conversion

Attempt a best-effort conversion in a pipeline where invalid values should not immediately abort processing.

Interview questions

QHow do you use astype for whole-frame and per-column conversion?

Pass a single dtype such as 'int32' to cast the entire DataFrame, or pass a mapping from column labels to dtypes to cast selected columns independently. The mapping can target one or more columns.

Follow-up: How can failed conversions be suppressed? Set errors='ignore'; the documentation defines this option as suppressing exceptions for invalid data.

QWhat forms can the dtype argument take?

dtype accepts a string, numpy dtype, pandas extension dtype, Python type, Series, or a mapping of column names to target dtypes. A mapping is useful when columns need different representations.

Follow-up: What happens to unmapped columns? They remain unchanged, so the mapping need not mention every column.

QWhat does astype return, and how does it handle invalid data?

The method returns a DataFrame containing the converted data. By default, errors='raise', so invalid data for the requested dtype produces an exception rather than silently proceeding.

Follow-up: When might errors='ignore' be useful? It can let a pipeline continue when conversion is opportunistic, though callers should verify the resulting dtypes.

  • pandas.DataFrame.convert_dtypes
  • pandas.DataFrame.dtypes
  • pandas.Series.astype

Source: pandas.DataFrame.astype. Checked 2026-10-15.