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.

At first, import the required libraries −

import pandas as pd
import numpy as np

Creating the index. For NaN, we have used numpy library −

index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship', None, None])

Display the index −

print("Pandas Index...\n",index)

Return a string of the type inferred from the values −

print("\nThe inferred type...\n",index.inferred_type)

Example

Following is the code −

import pandas as pd
import numpy as np

# Creating the index
# For NaN, we have used numpy library
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship', None, None])

# Display the index
print("Pandas Index...\n",index)

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

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

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

# Return a string of the type inferred from the values
print("\nThe inferred type...\n",index.inferred_type)

Output

This will produce the following code −

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

Updated on: 13-Oct-2021

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements