Pandas DataFrame | convert_dtypes method
Start your free 7-days trial now!
Pandas DataFrame.convert_dtypes(~)
method converts the data type of the columns of the source DataFrame to a more suitable/specific one.
This method returns a new DataFrame, that is, the source DataFrame is kept intact.
Parameters
1. infer_objects
| boolean
| optional
Whether or not to convert object
dtypes to a more suitable and specific dtype. By default, infer_objects=True
.
2. convert_string
| boolean
| optional
Whether or not to convert object
dtypes to a string dtype. By default, convert_string=True
.
3. convert_integer
| boolean
| optional
Whether or not to convert to integer dtypes, if possible. By default, convert_integer=True
.
4. convert_boolean
| boolean
| optional
Whether or not to convert object
types to boolean dtype. By default, convert_boolean=True
.
Return Value
A DataFrame with the converted dtypes.
Examples
Consider the following DataFrame:
df = pd.DataFrame({"A":["alex",np.NaN, "bob"],"B":[10,20,np.NaN], "C":[10.5,15.0,np.NaN], "D":[np.NaN,True,False]})df
A B C D0 alex 10.0 10.5 NaN1 NaN 20.0 15.0 True2 bob NaN NaN False
Our df
has the following dtypes:
df.dtypes
A objectB float64C float64D objectdtype: object
Here, note the following:
we end up with
object
types for columnsA
andD
even though they holdstring
andboolean
values, respectively. This is because they containNaN
, which ends up making the entire column typeobject
.column
B
can be represented by aint
instead of afloat
.
To make the data types of df
more specific, use the convert_dtypes(~)
method:
df_converted = df.convert_dtypes()df_converted
A B C D0 alex 10 10.5 <NA>1 <NA> 20 15.0 True2 bob <NA> NaN False
Now, the converted data type are as follows:
df_converted.dtypes
A stringB Int64C float64D booleandtype: object
Here, note the following:
the types of columns
A
andD
have been converted tostring
andboolean
, respectively.column
B
is nowint
instead offloat
.