Python - Check if the Pandas Index only consists of numeric data

To check if a Pandas Index consists only of numeric data, use the is_numeric() method. This method returns True if the index contains only numeric values (integers, floats, and NaNs), and False otherwise.

Syntax

index.is_numeric()

Creating a Numeric Index

Let's create a Pandas index with integers, floats, and NaNs ?

import pandas as pd
import numpy as np

# Creating Pandas index with integer, float and NaNs
index = pd.Index([5, 10.2, 25, 50, 75.2, 100, np.nan])

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

# Check whether index values has only numeric data
print("\nIndex values only consists of numeric data?")
print(index.is_numeric())
Pandas Index...
Float64Index([5.0, 10.2, 25.0, 50.0, 75.2, 100.0, nan], dtype='float64')

Index values only consists of numeric data?
True

Testing with Mixed Data Types

Now let's test with an index that contains both numeric and string values ?

import pandas as pd
import numpy as np

# Creating index with mixed data types
mixed_index = pd.Index([1, 2.5, 'text', 4, np.nan])

print("Mixed Index...")
print(mixed_index)

print("\nIs mixed index numeric?")
print(mixed_index.is_numeric())
Mixed Index...
Index([1, 2.5, 'text', 4, nan], dtype='object')

Is mixed index numeric?
False

Checking String Index

Let's verify with a purely string-based index ?

import pandas as pd

# Creating string index
string_index = pd.Index(['apple', 'banana', 'cherry'])

print("String Index...")
print(string_index)

print("\nIs string index numeric?")
print(string_index.is_numeric())
String Index...
Index(['apple', 'banana', 'cherry'], dtype='object')

Is string index numeric?
False

Key Points

  • The is_numeric() method considers integers, floats, and NaN values as numeric
  • Returns True only if ALL index values are numeric
  • Even one non-numeric value will make the method return False
  • The dtype of the index may change to 'object' when mixed data types are present

Conclusion

The is_numeric() method provides a quick way to validate if a Pandas Index contains only numeric data. Use this method for data validation and type checking in your data processing pipelines.

Updated on: 2026-03-26T16:01:12+05:30

673 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements