@@ -3610,7 +3610,7 @@ def blocks(self):
36103610 mapping = {True : 'raise' , False : 'ignore' })
36113611 def astype (self , dtype , copy = True , errors = 'raise' , ** kwargs ):
36123612 """
3613- Cast a pandas object to new dtype ``dtype``.
3613+ Cast a pandas object to a specified dtype ``dtype``.
36143614
36153615 Parameters
36163616 ----------
@@ -3620,7 +3620,9 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
36203620 column label and dtype is a numpy.dtype or Python type to cast one
36213621 or more of the DataFrame's columns to column-specific types.
36223622 copy : bool, default True.
3623- Return a copy when ``copy=True`` (be really careful with this!).
3623+ Return a copy when ``copy=True`` (be very careful setting
3624+ ``copy=False`` as changes to values then may propagate to other
3625+ pandas objects).
36243626 errors : {'raise', 'ignore'}, default 'raise'.
36253627 Control raising of exceptions on invalid data for provided dtype.
36263628
@@ -3650,15 +3652,15 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
36503652 1 2
36513653 dtype: int64
36523654
3653- Convert to pd.Categorial :
3655+ Convert to categorical type :
36543656
36553657 >>> ser.astype('category')
36563658 0 1
36573659 1 2
36583660 dtype: category
36593661 Categories (2, int64): [1, 2]
36603662
3661- Convert to ordered pd.Categorial with custom ordering:
3663+ Convert to ordered categorical type with custom ordering:
36623664
36633665 >>> ser.astype('category', ordered=True, categories=[2, 1])
36643666 0 1
@@ -3667,16 +3669,15 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
36673669 Categories (2, int64): [2 < 1]
36683670
36693671 Note that using ``copy=False`` and changing data on a new
3670- pandas object may propagate changes upwards:
3671-
3672- >>> cat1 = pd.Series([1,2], dtype='category')
3673- >>> cat2 = cat1.astype('category', copy=False)
3674- >>> cat2[0] = 2
3675- >>> cat1 # note that cat1[0] is changed too
3676- 0 2
3677- 1 2
3678- dtype: category
3679- Categories (2, int64): [1, 2]
3672+ pandas object may propagate changes:
3673+
3674+ >>> s1 = pd.Series([1,2])
3675+ >>> s2 = s1.astype('int', copy=False)
3676+ >>> s2[0] = 10
3677+ >>> s1 # note that s1[0] has changed too
3678+ 0 10
3679+ 1 2
3680+ dtype: int64
36803681
36813682 See also
36823683 --------
0 commit comments