Inspecting Column Data Types
Read the dtypes attribute, distinguish int64 from float64 and object, and spot columns that need conversion.
Why Column Data Types Matter
Every column in a Pandas DataFrame has a dtype (data type) that determines how values are stored in memory and which operations are valid on them. An integer column with dtype object (string) cannot be summed. A date stored as a string is treated as text, not a time series. Incorrect dtypes are one of the most common causes of silent bugs and unexpected results in data analysis pipelines.
Always inspect dtypes as the very first step after loading a new dataset.
import pandas as pd
import numpy as np
df = pd.DataFrame({
'age': ['25', '30', '35'], # looks like int, stored as object
'salary': [50000, 60000, 70000], # int64
'hired': ['2022-01-01', '2023-06-15', '2024-03-10'] # looks like date
})
print(df.dtypes)
# age object
# salary int64
# hired object
# dtype: objectThe dtypes Attribute
DataFrame.dtypes returns a Series whose index is the column names and whose values are the Pandas dtype of each column. Common dtypes you will encounter are int64, float64, object (strings and mixed), bool, datetime64[ns], and category. The dtype name tells you both the kind of data and how many bytes each value uses.
import pandas as pd
import numpy as np
df = pd.DataFrame({
'int_col': [1, 2, 3],
'float_col': [1.5, 2.5, 3.5],
'str_col': ['a', 'b', 'c'],
'bool_col': [True, False, True]
})
print(df.dtypes)
# int_col int64
# float_col float64
# str_col object
# bool_col bool
# dtype: objectAll lessons in this course
- Inspecting Column Data Types
- Casting with astype()
- Categorical Data Type
- Parsing Dates Correctly