Python Pandas - Return a string of the type inferred from the values

To return a string of the type inferred from the values, use the index.inferred_type property in Pandas. This property analyzes the data and returns a string indicating the inferred data type.

Syntax

index.inferred_type

Basic Example

Let's create an index with mixed data types and see how Pandas infers the type ?

import pandas as pd
import numpy as np

# Creating an index with mixed string and NaN values
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship', None, None])

# Display the index
print("Pandas Index...")
print(index)

# Return a string of the type inferred from the values
print("\nThe inferred type:")
print(index.inferred_type)
Pandas Index...
Index(['Car', 'Bike', nan, 'Car', nan, 'Ship', None, None], dtype='object')

The inferred type:
mixed

Different Data Types

Let's explore how different data types are inferred ?

import pandas as pd
import numpy as np

# Integer index
int_index = pd.Index([1, 2, 3, 4, 5])
print("Integer index inferred type:", int_index.inferred_type)

# Float index
float_index = pd.Index([1.1, 2.2, 3.3, 4.4])
print("Float index inferred type:", float_index.inferred_type)

# String index
str_index = pd.Index(['apple', 'banana', 'cherry'])
print("String index inferred type:", str_index.inferred_type)

# Boolean index
bool_index = pd.Index([True, False, True, False])
print("Boolean index inferred type:", bool_index.inferred_type)
Integer index inferred type: integer
Float index inferred type: floating
String index inferred type: string
Boolean index inferred type: boolean

Complete Example

Here's a comprehensive example showing various properties of a mixed-type index ?

import pandas as pd
import numpy as np

# Creating the index with mixed data types
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship', None, None])

# Display the index
print("Pandas Index...")
print(index)

# Return an array representing the data in the Index
print("\nArray...")
print(index.values)

# Check if the index is having NaNs
print("\nIs the Pandas index having NaNs?")
print(index.hasnans)

# Return the dtype of the data
print("\nThe dtype object...")
print(index.dtype)

# Return a string of the type inferred from the values
print("\nThe inferred type...")
print(index.inferred_type)
Pandas Index...
Index(['Car', 'Bike', nan, 'Car', nan, 'Ship', None, None], dtype='object')

Array...
['Car' 'Bike' nan 'Car' nan 'Ship' None None]

Is the Pandas index having NaNs?
True

The dtype object...
object

The inferred type...
mixed

Common Inferred Types

Inferred Type Description Example
integer All integer values [1, 2, 3, 4]
floating All floating-point values [1.1, 2.2, 3.3]
string All string values ['a', 'b', 'c']
boolean All boolean values [True, False, True]
mixed Mixed types or contains NaN/None ['Car', NaN, None]

Conclusion

The inferred_type property helps identify the data type that Pandas infers from an index. It returns "mixed" when the index contains different data types or missing values.

Updated on: 2026-03-26T16:14:03+05:30

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements