Skip to content

DOC: astype now takes dict mapping col names to datatypes (#14761) #14837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12 changes: 12 additions & 0 deletions doc/source/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,7 @@ then the more *general* one will be used as the result of the operation.
# conversion of dtypes
df3.astype('float32').dtypes


Convert a subset of columns to a specified type using :meth:`~DataFrame.astype`

.. ipython:: python
Expand All @@ -1766,6 +1767,17 @@ Convert a subset of columns to a specified type using :meth:`~DataFrame.astype`
dft
dft.dtypes

.. versionadded:: 0.19.0

Convert certain columns to a specific dtype by passing a dict to :meth:`~DataFrame.astype`

.. ipython:: python

dft1 = pd.DataFrame({'a': [1,0,1], 'b': [4,5,6], 'c': [7, 8, 9]})
dft1 = dft1.astype({'a': np.bool, 'c': np.float64})
dft1
dft1.dtypes

.. note::

When trying to convert a subset of columns to a specified type using :meth:`~DataFrame.astype` and :meth:`~DataFrame.loc`, upcasting occurs.
Expand Down