-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: Added an example of pitfalls when using astype #13278
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 3 commits
705e1a5
f394045
e1877bf
278e922
c30209d
035a177
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 |
---|---|---|
|
@@ -1726,6 +1726,28 @@ 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 | ||
|
||
df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]}) | ||
df[['a','b']] = df[['a','b']].astype(np.uint8) | ||
df | ||
df.dtypes | ||
|
||
.. note:: | ||
|
||
When trying to convert a subset of columns to a specified type using :meth:`~DataFrame.astype` and :meth:`~DataFrame.loc`, utilizing **:** as mask, upcasting occurs. | ||
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.
|
||
|
||
:meth:`~DataFrame.loc` tries to fit in what we are assigning to the current dtypes, while [ ] will overwrite them taking the dtype from the right hand side. Therefore the following piece of code produces the unintended result. | ||
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. double backticks around |
||
|
||
.. ipython:: python | ||
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 indent this one (and the following lines) to the same level as 'When trying to convert ...', so the code example is also included in the 'note' frame |
||
|
||
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. flip these around showing the correct method of assignment using astype. then you can put a |
||
df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]}) | ||
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. you already created the df, no need to recreate 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. df get's changed in the code example before. |
||
df.loc[:, ['a', 'b']].astype(np.uint8).dtypes | ||
df.loc[:, ['a', 'b']] = df.loc[:, ['a', 'b']].astype(np.uint8) | ||
df.dtypes | ||
|
||
object conversion | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
|
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.
call this
dft
or something besides df (as that's used a lot)