Skip to content

Commit a41eb2f

Browse files
author
tp
committed
Add examples to NDFrame.astype docs
1 parent 3c833db commit a41eb2f

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

pandas/core/generic.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,8 +3610,9 @@ def blocks(self):
36103610
mapping={True: 'raise', False: 'ignore'})
36113611
def astype(self, dtype, copy=True, errors='raise', **kwargs):
36123612
"""
3613-
Cast object to input numpy.dtype
3614-
Return a copy when copy = True (be really careful with this!)
3613+
Cast object to input numpy.dtype.
3614+
3615+
Return a copy when ``copy=True`` (be really careful with this!).
36153616
36163617
Parameters
36173618
----------
@@ -3636,6 +3637,34 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
36363637
Returns
36373638
-------
36383639
casted : type of caller
3640+
3641+
Examples
3642+
--------
3643+
>>> ser = pd.Series([1, 2], dtype='int32')
3644+
>>> ser
3645+
0 1
3646+
1 2
3647+
dtype: int32
3648+
>>> ser.astype('int64')
3649+
0 1
3650+
1 2
3651+
dtype: int64
3652+
3653+
Convert to pd.Categorial:
3654+
3655+
>>> ser.astype('category')
3656+
0 1
3657+
1 2
3658+
dtype: category
3659+
Categories (2, int64): [1, 2]
3660+
3661+
Convert to ordered pd.Categorial with custom ordering:
3662+
3663+
>>> ser.astype('category', ordered=True, categories=[2, 1])
3664+
0 1
3665+
1 2
3666+
dtype: category
3667+
Categories (2, int64): [2 < 1]
36393668
"""
36403669
if is_dict_like(dtype):
36413670
if self.ndim == 1: # i.e. Series

0 commit comments

Comments
 (0)