🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.dtypes

AI & DATA SCIENCE // df-dtypes

df.dtypes returns a Series listing the data type of each column in a DataFrame.

Syntax

df.dtypes

Deep Dive Course

dtypes is an attribute, not a method, returning a Series indexed by column name, with each value being that column's dtype — int64 for whole numbers, float64 for decimals, object for text, or genuinely mixed-type data, datetime64 for dates, and so on. It's the standard way to check whether a column ended up with the type you expect after loading or transforming data, since a column you expect to be numeric silently ending up as object usually signals a data-quality problem, like an unexpected non-numeric value mixed in.

1Understanding df.dtypes

dtypes is an attribute, not a method, returning a Series indexed by column name, with each value being that column's dtype — int64 for whole numbers, float64 for decimals, object for text, or genuinely mixed-type data, datetime64 for dates, and so on. It's the standard way to check whether a column ended up with the type you expect after loading or transforming data, since a column you expect to be numeric silently ending up as object usually signals a data-quality problem, like an unexpected non-numeric value mixed in.

💡

A numeric column unexpectedly showing dtype 'object' is a strong signal that it contains at least one non-numeric value somewhere — pandas falls back to the generic object dtype whenever a column can't be cleanly represented as a single numeric type.

editor.html
import pandas as pd

df = pd.DataFrame({"id": [1, 2, 3], "price": [9.99, 19.99, 29.99], "name": ["a", "b", "c"]})
print(df.dtypes)
localhost:3000

2Practical Example

Here is a real-world application of df.dtypes showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"quantity": [1, 2, "three"]})
print(df.dtypes)
localhost:3000

3Best Practices

Follow these guidelines when working with df.dtypes:

1. Check df.dtypes right after loading data to confirm columns have the expected types, especially numeric and date columns

2. Investigate immediately if a column you expect to be numeric shows dtype 'object' — it usually means a stray non-numeric value is mixed into that column

3. Use df.astype() to explicitly convert a column's dtype once you've identified and cleaned up the underlying data issue

⚠️

Tip: A numeric column unexpectedly showing dtype 'object' is a strong signal that it contains at least one non-numeric value somewhere — pandas falls back to the generic object dtype whenever a column can't be cleanly represented as a single numeric type.

editor.html
import pandas as pd

df = pd.DataFrame({"id": [1, 2, 3], "price": [9.99, 19.99, 29.99], "name": ["a", "b", "c"]})
print(df.dtypes)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"id": [1, 2, 3], "price": [9.99, 19.99, 29.99], "name": ["a", "b", "c"]})
print(df.dtypes)
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"quantity": [1, 2, "three"]})
print(df.dtypes)

Best Practices

  • Check df.dtypes right after loading data to confirm columns have the expected types, especially numeric and date columns
  • Investigate immediately if a column you expect to be numeric shows dtype 'object' — it usually means a stray non-numeric value is mixed into that column
  • Use df.astype() to explicitly convert a column's dtype once you've identified and cleaned up the underlying data issue

Interview Question

Why does a column containing a mix of integers and one genuine string end up with dtype 'object' instead of int64?

Hint: Think about what NumPy/pandas requires of a column's underlying storage.

A column's underlying storage, like a NumPy array, requires every element to share a single, consistent dtype — it can't store some elements as native integers and others as native strings side by side. Since the column mixes actual integers with a genuine string, pandas can't represent it as a clean numeric dtype like int64, so it falls back to the generic object dtype, which stores each element as a reference to an arbitrary Python object instead of a fixed-size native numeric type, sacrificing the performance benefits of a proper numeric dtype for the flexibility of holding mixed types.

Exercises

MediumPractice using df.dtypes in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"id": [1, 2, 3], "price": [9.99, 19.99, 29.99], "name": ["a", "b", "c"]})
print(df.dtypes)

Frequently Asked Questions

Why does a column containing a mix of integers and one genuine string end up with dtype 'object' instead of int64?

A column's underlying storage, like a NumPy array, requires every element to share a single, consistent dtype — it can't store some elements as native integers and others as native strings side by side. Since the column mixes actual integers with a genuine string, pandas can't represent it as a clean numeric dtype like int64, so it falls back to the generic object dtype, which stores each element as a reference to an arbitrary Python object instead of a fixed-size native numeric type, sacrificing the performance benefits of a proper numeric dtype for the flexibility of holding mixed types.

Related Functions

df-astypendarray-dtypedf-info