-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 1 commit
3f5d728
fddbb2e
a61ec51
e714d8c
136175c
6282de6
0c90785
bc65ff4
b9414a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
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}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: ..." There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will leave this file unchanged. |
||
.. _whatsnew_0200.enhancements.other: | ||
|
||
Other enhancements | ||
|
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
I think I'm missing what new functionality has been introduced in v0.20
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.