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

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

.. versionadded:: 0.20.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.20 -> 0.19

and I would also clarify what was added in 0.20.0 (only the ability to specify different dtypes for different columns with a dict)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused. I tried using a dict with column/dtype mapping as an
argument to astype and this works in 0.19.0:

>>> import numpy as np
>>> import pandas as pd
>>> pd.__version__
'0.19.0'
>>> dft = pd.DataFrame({'a': [1,0,1], 'b': [4,5,6], 'c': [7, 8, 9]})
>>> dft = dft.astype({'a': np.bool, 'c': np.uint8})
>>> dft
       a  b  c
0   True  4  7
1  False  5  8
2   True  6  9
>>> dft.dtypes
a     bool
b    int64
c    uint8
dtype: object

I think I'm missing what new functionality has been introduced in v0.20

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-charlton As I said above, this feature was already introduced in 0.19.0. That's the reason I asked to change 0.20.0 to 0.19.0 in the versionadded here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Will submit changes today.


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

.. ipython:: python

dft = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]})
dft[['a','b']] = dft[['a','b']].astype(np.uint8)
dft = dft.astype({'a': np.float64, 'c': np.uint8})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave the previous example there as well, and add this line (the single dtype is still a valid case)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I would leave this example, but just add a new one below this one, also beginning with an explaining sentence, like "Convert certain columns to as specific dtype by passing a dict to astype: ..."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will make relevant changes and resubmit. Old example will be left in place as is.

dft
dft.dtypes

Expand Down
10 changes: 10 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ fixed-width text files, and :func:`read_excel` for parsing Excel files.
pd.read_fwf(StringIO(data)).dtypes
pd.read_fwf(StringIO(data), dtype={'a':'float64', 'b':'object'}).dtypes

You can now pass a dictionary mapping column names to desired data types for that
column to :meth:`~DataFrame.astype`.

.. ipython:: python

dft = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]})
dft = dft.astype({'a': np.float64, 'c': np.uint8})
dft
dft.dtypes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-charlton This is not needed, as this enhancement was already added in 0.19.0, and is mentioned in the whatsnew of 0.19.0 (see the changes in the PR https://github.com/pandas-dev/pandas/pull/13375/files)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will leave this file unchanged.

.. _whatsnew_0200.enhancements.other:

Other enhancements
Expand Down