w3resource

Pandas Series: infer_objects() function

Better dtypes for object columns

Attempt to infer better dtypes for object columns.

Attempts soft conversion of object-dtyped columns, leaving non-object and unconvertible columns unchanged. The inference rules are the same as during normal Series/DataFrame construction.

Syntax:

Series.infer_objects(self)
Pandas Series infer_objects() function

Returns: converted - same type as input object

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({"X": ["p", 2, 3, 4]})
df = df.iloc[1:]
df

Output:

   X
1  2
2  3
3  4

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({"X": ["p", 2, 3, 4]})
df = df.iloc[1:]
df.dtypes

Output:

X    object
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({"X": ["p", 2, 3, 4]})
df = df.iloc[1:]
df.infer_objects().dtypes

Output:

X    int64
dtype: object

Previous: Change data type of a series in Pandas
Next: Copy of Pandas object



Follow us on Facebook and Twitter for latest update.