8000 DOCS: Updated NDFrame.astype docs by topper-123 · Pull Request #17203 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

DOCS: Updated NDFrame.astype docs #17203

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

Merged
merged 5 commits into from
Aug 9, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
NDFrame.astype doc string, some further cleanup
  • Loading branch information
tp committed Aug 9, 2017
commit c90160ad67ac82cb1c4f6952be7ca333520b5b3a
29 changes: 15 additions & 14 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3610,7 +3610,7 @@ def blocks(self):
mapping={True: 'raise', False: 'ignore'})
def astype(self, dtype, copy=True, errors='raise', **kwargs):
"""
Cast a pandas object to new dtype ``dtype``.
Cast a pandas object to a specified dtype ``dtype``.

Parameters
----------
Expand All @@ -3620,7 +3620,9 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
column label and dtype is a numpy.dtype or Python type to cast one
or more of the DataFrame's columns to column-specific types.
copy : bool, default True.
Return a copy when ``copy=True`` (be really careful with this!).
Return a copy when ``copy=True`` (be very careful setting
``copy=False`` as changes to values then may propagate to other
pandas objects).
Copy link
Member

Choose a reason for hiding this comment

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

maybe add something like ", and copy=False has only effect when the specified dtype is equivalent to the existing dtype" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I consciously made it clearer but not-too-specific, as I don't really know how copy does its thing, and I doubt this is used so often.

I suggest to leave it as it is. Is that ok?

Copy link
Member

Choose a reason for hiding this comment

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

yes, that is fine

errors : {'raise', 'ignore'}, default 'raise'.
Control raising of exceptions on invalid data for provided dtype.

Expand Down Expand Up @@ -3650,15 +3652,15 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
1 2
dtype: int64

Convert to pd.Categorial:
Convert to categorical type:

>>> ser.astype('category')
0 1
1 2
dtype: category
Categories (2, int64): [1, 2]

Convert to ordered pd.Categorial with custom ordering:
Convert to ordered categorical type with custom ordering:

>>> ser.astype('category', ordered=True, categories=[2, 1])
0 1
Expand All @@ -3667,16 +3669,15 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
Categories (2, int64): [2 < 1]

Note that using ``copy=False`` and changing data on a new
pandas object may propagate changes upwards:

>>> cat1 = pd.Series([1,2], dtype='category')
>>> cat2 = cat1.astype('category', copy=False)
>>> cat2[0] = 2
>>> cat1 # note that cat1[0] is changed too
0 2
1 2
dtype: category
Categories (2, int64): [1, 2]
pandas object may propagate changes:

>>> s1 = pd.Series([1,2])
>>> s2 = s1.astype('int', copy=False)
>>> s2[0] = 10
>>> s1 # note that s1[0] has changed too
0 10
1 2
dtype: int64

See also
--------
Expand Down
0