search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | convert_dtypes method

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
tocTable of Contents
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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 D
0 alex 10.0 10.5 NaN
1 NaN 20.0 15.0 True
2 bob NaN NaN False

Our df has the following dtypes:

df.dtypes
A object
B float64
C float64
D object
dtype: object

Here, note the following:

  • we end up with object types for columns A and D even though they hold string and boolean values, respectively. This is because they contain NaN, which ends up making the entire column type object.

  • column B can be represented by a int instead of a float.

To make the data types of df more specific, use the convert_dtypes(~) method:

df_converted = df.convert_dtypes()
df_converted
A B C D
0 alex 10 10.5 <NA>
1 <NA> 20 15.0 True
2 bob <NA> NaN False

Now, the converted data type are as follows:

df_converted.dtypes
A string
B Int64
C float64
D boolean
dtype: object

Here, note the following:

  • the types of columns A and D have been converted to string and boolean, respectively.

  • column B is now int instead of float.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!