@@ -3610,7 +3610,7 @@ def blocks(self):
3610
3610
mapping = {True : 'raise' , False : 'ignore' })
3611
3611
def astype (self , dtype , copy = True , errors = 'raise' , ** kwargs ):
3612
3612
"""
3613
- Cast a pandas object to new dtype ``dtype``.
3613
+ Cast a pandas object to a specified dtype ``dtype``.
3614
3614
3615
3615
Parameters
3616
3616
----------
@@ -3620,7 +3620,9 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
3620
3620
column label and dtype is a numpy.dtype or Python type to cast one
3621
3621
or more of the DataFrame's columns to column-specific types.
3622
3622
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).
3624
3626
errors : {'raise', 'ignore'}, default 'raise'.
3625
3627
Control raising of exceptions on invalid data for provided dtype.
3626
3628
@@ -3650,15 +3652,15 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
3650
3652
1 2
3651
3653
dtype: int64
3652
3654
3653
- Convert to pd.Categorial :
3655
+ Convert to categorical type :
3654
3656
3655
3657
>>> ser.astype('category')
3656
3658
0 1
3657
3659
1 2
3658
3660
dtype: category
3659
3661
Categories (2, int64): [1, 2]
3660
3662
3661
- Convert to ordered pd.Categorial with custom ordering:
3663
+ Convert to ordered categorical type with custom ordering:
3662
3664
3663
3665
>>> ser.astype('category', ordered=True, categories=[2, 1])
3664
3666
0 1
@@ -3667,16 +3669,15 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
3667
3669
Categories (2, int64): [2 < 1]
3668
3670
3669
3671
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
3680
3681
3681
3682
See also
3682
3683
--------
0 commit comments