I have a data source where all the values are given as strings. When I create a Pandas dataframe from this data, all the columns are naturally of type object
. I then want to let Pandas automatically convert any columns that look like numbers into a numeric types (e.g. int64
, float64
).
Pandas supposedly provides a function to do this automatic type inferencing: pandas.DataFrame.infer_objects()
. It's also mentioned in this StackOverflow post. The documentation says:
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.
However, the function is not working for me. In the reproducible example below, I have two string columns (value1
and value2
) that unambiguously look like int
and float
values, respectively, but infer_objects()
does not convert them from string to the appropriate numeric types.
import pandas as pd
# Create example dataframe.
data = [ ['Alice', '100', '1.1'], ['Bob', '200', '2.1'], ['Carl', '300', '3.1']]
df = pd.DataFrame(data, columns=['name', 'value1', 'value2'])
print(df)
# name value1 value2
# 0 Alice 100 1.1
# 1 Bob 200 2.1
# 2 Carl 300 3.1
print(df.info())
# Data columns (total 3 columns):
# # Column Non-Null Count Dtype
# --- ------ -------------- -----
# 0 name 3 non-null object
# 1 value1 3 non-null object
# 2 value2 3 non-null object
# dtypes: object(3)
df = df.infer_objects() # Should convert value1 and value2 columns to numerics.
print(df.info())
# Data columns (total 3 columns):
# # Column Non-Null Count Dtype
# --- ------ -------------- -----
# 0 name 3 non-null object
# 1 value1 3 non-null object
# 2 value2 3 non-null object
# dtypes: object(3)
Any help would be appreciated.