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

Conversation

m-charlton
Copy link
Contributor

@m-charlton m-charlton commented Dec 9, 2016

Updating documentation to reflect change

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

@m-charlton Thanks for working on this!
I added a few comments (which I now see you already asked about this in the issue, but only see it now)

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

@jorisvandenbossche jorisvandenbossche added this to the 0.20.0 milestone Dec 9, 2016
@codecov-io
Copy link

codecov-io commented Dec 9, 2016

Current coverage is 84.64% (diff: 100%)

Merging #14837 into master will decrease coverage by <.01%

@@             master     #14837   diff @@
==========================================
  Files           144        144          
  Lines         51016      51016          
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
- Hits          43185      43184     -1   
- Misses         7831       7832     +1   
  Partials          0          0          

Powered by Codecov. Last update 8e630b6...b9414a9

@jorisvandenbossche jorisvandenbossche changed the title astype method now takes dict mapping col names to datatypes #14761 DOC: astype now takes dict mapping col names to datatypes (#14761) Dec 9, 2016
@m-charlton m-charlton closed this Dec 15, 2016
@m-charlton m-charlton deleted the update_docs_astype_with_dict_#14761 branch December 15, 2016 11:25
@jorisvandenbossche
Copy link
Member

@m-charlton you don't need to close this PR or remove the branch on github to update this. You can just push to the same branch, and the PR on github will automatically be updated.
If you restore the branch, reopen the PR, and then push again, it should still work like that.

@m-charlton m-charlton restored the update_docs_astype_with_dict_#14761 branch December 15, 2016 13:31
@m-charlton m-charlton reopened this Dec 15, 2016
DataFrame.astype now allows changing the dtype of a column by
passing a dict mapping column name to dtype.
DataFrame.astype now allows setting the type of columns
by passing a dict mapping column to dtype.
@jorisvandenbossche
Copy link
Member

@m-charlton Some git help, seems something went wrong with updating to master. If you do the following, it should normally be solved:

git fetch upstream
git checkout update_docs_astype_with_dict_#14761
git rebase upstream/master
git push -f 

@m-charlton
Copy link
Contributor Author

I performed the above before I saw your git help.
Has this fixed the problem?

@jreback jreback removed this from the 0.20.0 milestone Dec 16, 2016
Mistakenly added changes carried out in v0.19 to v0.20
Ability to pass dict mapping column to dtype
introduced in v0.19 not v0.20
@m-charlton m-charlton force-pushed the update_docs_astype_with_dict_#14761 branch from 5f6db38 to b9414a9 Compare December 19, 2016 22:14
@m-charlton
Copy link
Contributor Author

I think I've rectified my mistakes in update b9414a9

@jreback jreback added this to the 0.20.0 milestone Dec 19, 2016
@jreback
Copy link
Contributor

jreback commented Dec 19, 2016

lgtm. ping on green.

@m-charlton
Copy link
Contributor Author

I think it was more to do with the content so @jorisvandenbossche needs to
give it a look.

@jorisvandenbossche jorisvandenbossche merged commit 8b497e4 into pandas-dev:master Dec 22, 2016
@m-charlton m-charlton deleted the update_docs_astype_with_dict_#14761 branch December 22, 2016 15:18
ShaharBental pushed a commit to ShaharBental/pandas that referenced this pull request Dec 26, 2016
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.

4 participants