Converting columns to the correct data type is essential for proper calculations, memory efficiency, and avoiding unexpected behavior. Pandas provides several methods depending on your data quality and conversion needs.
Using astype() - Direct Conversion
The most straightforward method for clean data:
import pandas as pd
df = pd.DataFrame({
'ID': [1, 2, 3],
'Price': ['10.5', '20.0', '15.75']
})
print(df.dtypes)
print()
df['Price'] = df['Price'].astype(float)
print(df.dtypes)Output:
ID int64
Price object
dtype: object
ID int64
Price float64
dtype: objectConvert Multiple Columns
import pandas as pd
df = pd.DataFrame({
'ID': [1, 2, 3],
'Price': ['10.5', '20.0', '15.75'],
'Active': [1, 0, 1]
})
df = df.astype({
'ID': str,
'Price': float,
'Active': bool
})
print(df.dtypes)Output:
ID object
Price float64
Active bool
dtype: objectUsing pd.to_numeric() - Safe Conversion
Handles messy data with invalid values:
import pandas as pd
df = pd.DataFrame({
'Price': ['10.5', '20.0', 'N/A', '15.75', 'unknown']
})
df['Price'] = pd.to_numeric(df['Price'], errors='coerce')
print(df)Output:
Price
0 10.50
1 20.00
2 NaN
3 15.75
4 NaNError Handling Options
import pandas as pd
s = pd.Series(['1', '2', 'bad', '4'])
pd.to_numeric(s, errors='coerce') # [1.0, 2.0, NaN, 4.0]
pd.to_numeric(s, errors='ignore') # ['1', '2', 'bad', '4']Using pd.to_datetime() - Date Conversion
Robust parsing for date strings:
import pandas as pd
df = pd.DataFrame({
'Date': ['2024-01-15', '2024/02/20', 'March 10, 2024']
})
df['Date'] = pd.to_datetime(df['Date'], format='mixed')
print(df.dtypes)Output:
Date datetime64[ns]
dtype: objectHandling Date Errors
import pandas as pd
df = pd.DataFrame({
'Date': ['2024-01-15', 'invalid', '2024-03-20']
})
df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
print(df)Output:
Date
0 2024-01-15
1 NaT
2 2024-03-20Common Type Conversions
| From | To | Method |
|---|---|---|
| String → Integer | int |
astype(int) or pd.to_numeric() |
| String → Float | float |
astype(float) or pd.to_numeric() |
| String → DateTime | datetime64 |
pd.to_datetime() |
| Integer → String | str |
astype(str) |
| Float → Integer | int |
astype(int) (truncates) |
| Any → Boolean | bool |
astype(bool) |
| Any → Category | category |
astype('category') |
Memory Optimization with Downcasting
import pandas as pd
df = pd.DataFrame({'Count': [1, 2, 3, 4, 5]})
print(f"Before: {df['Count'].dtype}") # int64 (8 bytes)
df['Count'] = pd.to_numeric(df['Count'], downcast='integer')
print(f"After: {df['Count'].dtype}") # int8 (1 byte)Output:
Before: int64
After: int8Handling Conversion Failures
import pandas as pd
df = pd.DataFrame({'Value': ['10', '20', 'thirty']})
try:
df['Value'].astype(int)
except ValueError as e:
print(f"Error: {e}")
df['Value'] = pd.to_numeric(df['Value'], errors='coerce')
print(df)Output:
Error: invalid literal for int() with base 10: 'thirty'
Value
0 10.0
1 20.0
2 NaNQuick Reference
| Method | Best For | On Invalid Data |
|---|---|---|
astype() |
Clean, validated data | Raises ValueError |
pd.to_numeric() |
Numeric with possible errors | NaN (with coerce) |
pd.to_datetime() |
Date strings, various formats | NaT (with coerce) |
Summary
- Use
astype()for quick conversions on clean data-it's fast and explicit. - Use
pd.to_numeric()orpd.to_datetime()when handling messy real-world data where some values might be invalid; theerrors='coerce'option converts problematic values toNaN/NaTinstead of crashing your pipeline.