-
-
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
Conversation
@@ -1726,6 +1726,23 @@ then the more *general* one will be used as the result of the operation. | |||
# conversion of dtypes | |||
df3.astype('float32').dtypes | |||
|
|||
When trying to convert a subset of columns to a specified type using :meth:`~DataFrame.astype` and :meth:`~numpy.ndarray.loc`, utilizing **:** as mask, upcasting occurs. |
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.
:meth:
~numpy.ndarray.loc -> `:meth:`~DataFrame.loc
@pfrcks Thanks for the contribution! Personally, I would reverse the logic in the explanation, by first saying how you should do it, and only then what does not work. Eg in the sense of "When you want to astype subset of columns -> you have to do it like this: ... -> attention, you cannot use loc in this case like ... as this upcasts" |
@jorisvandenbossche I have made the cleanup changes you recommended. Do I need to change the order of the wrong and right way of doing things? |
To avoid this please take the following approach. | ||
|
||
.. ipython:: python | ||
|
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.
flip these around showing the correct method of assignment using astype.
then you can put a ::note
for the explanation. People reading this just want the answer; some want the explanation, so highliting that differently helps.
Restructured the documentation. |
|
||
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. | ||
|
||
: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 comment
The reason will be displayed to describe this comment to others. Learn more.
double backticks around []
|
||
.. 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 comment
The reason will be displayed to describe this comment to others. Learn more.
utilizing **:** as mask
is not needed I think (in the text), as you cannot assign columns using loc without it.
@jorisvandenbossche Made the suggested changes. Hope it is fine now. |
|
||
: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. | ||
|
||
.. ipython:: python |
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.
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
Only a small last comment, for the rest looks good! |
@jorisvandenbossche I hope that does the trick ;) |
@pfrcks Thanks! |
git diff upstream/master | flake8 --diff