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)
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
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/pandas/series/series-infer_objects.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics