Skip to content

Commit b4d4ec5

Browse files
stevennicTomAugspurger
authored andcommitted
DOC: df.astype example using dictionary (#26994)
* DOC: df.astype example using dictionary (#26990)
1 parent 2b9b58d commit b4d4ec5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pandas/core/generic.py

+25
Original file line numberDiff line numberDiff line change
@@ -5622,6 +5622,31 @@ def astype(self, dtype, copy=True, errors='raise', **kwargs):
56225622
56235623
Examples
56245624
--------
5625+
Create a DataFrame:
5626+
5627+
>>> d = {'col1': [1, 2], 'col2': [3, 4]}
5628+
>>> df = pd.DataFrame(data=d)
5629+
>>> df.dtypes
5630+
col1 int64
5631+
col2 int64
5632+
dtype: object
5633+
5634+
Cast all columns to int32:
5635+
5636+
>>> df.astype('int32').dtypes
5637+
col1 int32
5638+
col2 int32
5639+
dtype: object
5640+
5641+
Cast col1 to int32 using a dictionary:
5642+
5643+
>>> df.astype({'col1': 'int32'}).dtypes
5644+
col1 int32
5645+
col2 int64
5646+
dtype: object
5647+
5648+
Create a series:
5649+
56255650
>>> ser = pd.Series([1, 2], dtype='int32')
56265651
>>> ser
56275652
0 1

0 commit comments

Comments
 (0)