Pandas DataFrame.infer_objects


Pandas DataFrame.infer_objects

The DataFrame.infer_objects method in pandas is used to attempt to infer better data types for object columns. This method is particularly useful when working with DataFrames that contain columns with generic object data types but can be converted to more specific types (e.g., int, float, or datetime).


Syntax

The syntax for DataFrame.infer_objects is:

DataFrame.infer_objects(copy=None)

Here, DataFrame refers to the pandas DataFrame whose object columns' types are being inferred.


Parameters

ParameterDescription
copyIf True, returns a new DataFrame with inferred types. If False, modifies the DataFrame in place. Defaults to None, which behaves as True.

Returns

A DataFrame with object columns converted to inferred data types, if possible.


Examples

Inferring Types in a DataFrame

Use infer_objects to convert object columns to specific types where possible.

Python Program

import pandas as pd

# Create a DataFrame with object columns
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': ['25', '30', '35'],
    'Salary': ['70000.5', '80000.0', '90000.0']
}
df = pd.DataFrame(data)

# Infer better types for object columns
print("Original DataFrame:")
print(df)
print("\nInferred DataFrame:")
df_inferred = df.infer_objects()
print(df_inferred)

Output

Original DataFrame:
    Name Age   Salary
0  Arjun  25  70000.5
1    Ram  30  80000.0
2   Priya  35  90000.0

Inferred DataFrame:
    Name  Age   Salary
0  Arjun   25  70000.5
1    Ram   30  80000.0
2   Priya   35  90000.0

Checking Data Types Before and After Inferring

You can compare the data types before and after using infer_objects to see the changes.

Python Program

import pandas as pd

# Create a DataFrame with object columns
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': ['25', '30', '35'],
    'JoiningDate': ['2022-01-01', '2021-05-12', '2020-08-15']
}
df = pd.DataFrame(data)

# Display dtypes before and after inferring
print("Data types before inferring:")
print(df.dtypes)

# Infer better types
df_inferred = df.infer_objects()

print("\nData types after inferring:")
print(df_inferred.dtypes)

Output

Data types before inferring:
Name           object
Age            object
JoiningDate    object
dtype: object

Data types after inferring:
Name                   object
Age                     int64
JoiningDate    datetime64[ns]
dtype: object

Using copy=False for In-Place Conversion

Set copy=False to attempt in-place inference without creating a new DataFrame.

Python Program

import pandas as pd

# Create a DataFrame with object columns
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': ['25', '30', '35'],
    'Salary': ['70000.5', '80000.0', '90000.0']
}
df = pd.DataFrame(data)

# Infer types in place
print("Before in-place inferring:")
print(df.dtypes)
df.infer_objects(copy=False)
print("\nAfter in-place inferring:")
print(df.dtypes)

Output

Before in-place inferring:
Name      object
Age       object
Salary    object
dtype: object

After in-place inferring:
Name      object
Age        int64
Salary   float64
dtype: object

Summary

In this tutorial, we explored the DataFrame.infer_objects method in pandas. Key takeaways include:

  • Using infer_objects to convert generic object columns to more specific types.
  • Comparing data types before and after using infer_objects.
  • Performing in-place type inference with copy=False.

The DataFrame.infer_objects method is a helpful tool for cleaning and optimizing data in pandas DataFrames.


Python Libraries