Skip to content

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

Closed
wants to merge 6 commits into from

Conversation

pfrcks
Copy link
Contributor

@pfrcks pfrcks commented May 25, 2016

@@ -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.
Copy link
Member

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

@jorisvandenbossche
Copy link
Member

@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 jorisvandenbossche added this to the 0.18.2 milestone May 25, 2016
@pfrcks
Copy link
Contributor Author

pfrcks commented May 25, 2016

@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?
Wrong followed by right way of doing things also sends a clear message I guess. We see how the thing is wrong, then we see how to correct or implement it properly.

To avoid this please take the following approach.

.. ipython:: python

Copy link
Contributor

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.

@pfrcks
Copy link
Contributor Author

pfrcks commented May 25, 2016

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.
Copy link
Contributor

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.
Copy link
Member

@jorisvandenbossche jorisvandenbossche May 26, 2016

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.

@pfrcks
Copy link
Contributor Author

pfrcks commented May 26, 2016

@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
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 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

@jorisvandenbossche
Copy link
Member

Only a small last comment, for the rest looks good!

@pfrcks
Copy link
Contributor Author

pfrcks commented May 26, 2016

@jorisvandenbossche I hope that does the trick ;)

@jorisvandenbossche
Copy link
Member

@pfrcks Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DOC: Strange behavior when combining astype and loc
3 participants