Skip to content

Commit

Permalink
DOC: Updated NDFrame.astype docs (pandas-dev#17203)
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 authored and jowens committed Sep 20, 2017
1 parent e71e6d7 commit e9c7f29
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3610,8 +3610,7 @@ def blocks(self):
mapping={True: 'raise', False: 'ignore'})
def astype(self, dtype, copy=True, errors='raise', **kwargs):
"""
Cast object to input numpy.dtype
Return a copy when copy = True (be really careful with this!)
Cast a pandas object to a specified dtype ``dtype``.
Parameters
----------
Expand All @@ -3620,6 +3619,10 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
the same type. Alternatively, use {col: dtype, ...}, where col is a
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 very careful setting
``copy=False`` as changes to values then may propagate to other
pandas objects).
errors : {'raise', 'ignore'}, default 'raise'.
Control raising of exceptions on invalid data for provided dtype.
Expand All @@ -3636,6 +3639,49 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
Returns
-------
casted : type of caller
Examples
--------
>>> ser = pd.Series([1, 2], dtype='int32')
>>> ser
0 1
1 2
dtype: int32
>>> ser.astype('int64')
0 1
1 2
dtype: int64
Convert to categorical type:
>>> ser.astype('category')
0 1
1 2
dtype: category
Categories (2, int64): [1, 2]
Convert to ordered categorical type with custom ordering:
>>> ser.astype('category', ordered=True, categories=[2, 1])
0 1
1 2
dtype: category
Categories (2, int64): [2 < 1]
Note that using ``copy=False`` and changing data on a new
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
--------
numpy.ndarray.astype : Cast a numpy array to a specified type.
"""
if is_dict_like(dtype):
if self.ndim == 1: # i.e. Series
Expand Down

0 comments on commit e9c7f29

Please sign in to comment.