0
0
PandasHow-ToBeginner · 3 min read

How to Use dtypes in pandas for Data Type Management

In pandas, dtypes shows the data type of each column in a DataFrame or Series. You can use df.dtypes to check types and df.astype() to convert columns to different types.
📐

Syntax

The main ways to work with dtypes in pandas are:

  • df.dtypes: Returns the data types of each column in a DataFrame.
  • df.astype(dtype): Converts one or more columns to the specified data type.

Here, df is your pandas DataFrame, and dtype can be a single type or a dictionary mapping columns to types.

python
df.dtypes

df.astype(dtype)

# Example:
df.astype({'col1': 'int32', 'col2': 'float64'})
💻

Example

This example shows how to check the data types of columns and convert a column from string to integer.

python
import pandas as pd

data = {'age': ['25', '30', '22'], 'name': ['Alice', 'Bob', 'Charlie']}
df = pd.DataFrame(data)

# Check original dtypes
print(df.dtypes)

# Convert 'age' column to integer

df['age'] = df['age'].astype(int)

# Check dtypes after conversion
print(df.dtypes)
Output
age object name object dtype: object age int64 name object dtype: object
⚠️

Common Pitfalls

Common mistakes when using dtypes include:

  • Trying to convert columns with invalid values (e.g., strings that can't be numbers) causes errors.
  • Not specifying the correct data type format, leading to unexpected results.
  • Forgetting that astype() returns a new DataFrame or Series unless you assign it back.

Always check your data before converting and assign the result back to keep changes.

python
import pandas as pd

data = {'score': ['10', 'twenty', '30']}
df = pd.DataFrame(data)

# This will raise an error because 'twenty' can't convert to int
try:
    df['score'] = df['score'].astype(int)
except ValueError as e:
    print(f"Error: {e}")

# Correct approach: clean or filter data before conversion
# For example, replace non-numeric with NaN and convert
import numpy as np

df['score'] = pd.to_numeric(df['score'], errors='coerce')
print(df)
print(df.dtypes)
Output
Error: invalid literal for int() with base 10: 'twenty' score 0 10.0 1 NaN 2 30.0 score float64 dtype: object
📊

Quick Reference

OperationDescriptionExample
Check dtypesView data types of DataFrame columnsdf.dtypes
Convert dtypeChange column typedf['col'] = df['col'].astype('int')
Convert multipleChange multiple columnsdf.astype({'col1': 'int', 'col2': 'float'})
Safe numeric convertConvert with errors handledpd.to_numeric(df['col'], errors='coerce')

Key Takeaways

Use df.dtypes to quickly see the data types of all columns in a DataFrame.
Convert column types with df.astype(), assigning the result back to keep changes.
Invalid conversions cause errors; clean or handle data before converting types.
pd.to_numeric() helps safely convert columns with non-numeric values by coercing errors.
Always verify data types after conversion to ensure correctness.