@@ -1757,17 +1757,27 @@ then the more *general* one will be used as the result of the operation.
1757
1757
# conversion of dtypes
1758
1758
df3.astype(' float32' ).dtypes
1759
1759
1760
- .. versionadded :: 0.20.0
1761
1760
1762
1761
Convert a subset of columns to a specified type using :meth: `~DataFrame.astype `
1763
1762
1764
1763
.. ipython :: python
1765
1764
1766
1765
dft = pd.DataFrame({' a' : [1 ,2 ,3 ], ' b' : [4 ,5 ,6 ], ' c' : [7 , 8 , 9 ]})
1767
- dft = dft.astype({ ' a' : np.float64, ' c ' : np.uint8} )
1766
+ dft[[ ' a ' , ' b ' ]] = dft[[ ' a' , ' b ' ]].astype( np.uint8)
1768
1767
dft
1769
1768
dft.dtypes
1770
1769
1770
+ .. versionadded :: 0.19.0
1771
+
1772
+ Convert certain columns to a specific dtype by passing a dict to :meth: `~DataFrame.astype `
1773
+
1774
+ .. ipython :: python
1775
+
1776
+ dft1 = pd.DataFrame({' a' : [1 ,0 ,1 ], ' b' : [4 ,5 ,6 ], ' c' : [7 , 8 , 9 ]})
1777
+ dft1 = dft1.astype({' a' : np.bool, ' c' : np.float64})
1778
+ dft1
1779
+ dft1.dtypes
1780
+
1771
1781
.. note ::
1772
1782
1773
1783
When trying to convert a subset of columns to a specified type using :meth: `~DataFrame.astype ` and :meth: `~DataFrame.loc `, upcasting occurs.
@@ -1781,18 +1791,6 @@ Convert a subset of columns to a specified type using :meth:`~DataFrame.astype`
1781
1791
dft.loc[:, [' a' , ' b' ]] = dft.loc[:, [' a' , ' b' ]].astype(np.uint8)
1782
1792
dft.dtypes
1783
1793
1784
-
1785
- .. versionadded :: 0.19
1786
-
1787
- Convert certain columns to a specific dtype by passing a dict to :meth: `~DataFrame.astype `
1788
-
1789
- .. ipython :: python
1790
-
1791
- dft1 = pd.DataFrame({' a' : [1 ,0 ,1 ], ' b' : [4 ,5 ,6 ], ' c' : [7 , 8 , 9 ]})
1792
- dft1 = dft1.astype({' a' : np.bool, ' c' : np.float64})
1793
- dft1
1794
- dft1.dtypes
1795
-
1796
1794
.. _basics.object_conversion :
1797
1795
1798
1796
object conversion
0 commit comments