Skip to content

Commit e1877bf

Browse files
committed
DOC: Restructured the documentation
1 parent f394045 commit e1877bf

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

doc/source/basics.rst

+11-7
Original file line numberDiff line numberDiff line change
@@ -1726,22 +1726,26 @@ then the more *general* one will be used as the result of the operation.
17261726
# conversion of dtypes
17271727
df3.astype('float32').dtypes
17281728
1729-
When trying to convert a subset of columns to a specified type using :meth:`~DataFrame.astype` and :meth:`~DataFrame.loc`, utilizing **:** as mask, upcasting occurs.
1730-
:meth:`~DataFrame.loc` tries to fit in what we are assigning to the current dtypes, while [ ] will overwrite them taking the dtype from the right hand side.
1729+
Convert a subset of columns to a specified type using :meth:`~DataFrame.astype`
17311730

17321731
.. ipython:: python
17331732
17341733
df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]})
1735-
df.loc[:, ['a', 'b']].astype(np.uint8).dtypes
1736-
df.loc[:, ['a', 'b']] = df.loc[:, ['a', 'b']].astype(np.uint8)
1734+
df[['a','b']] = df[['a','b']].astype(np.uint8)
1735+
df
1736+
df.dtypes
1737+
1738+
.. note::
1739+
1740+
When trying to convert a subset of columns to a specified type using :meth:`~DataFrame.astype` and :meth:`~DataFrame.loc`, utilizing **:** as mask, upcasting occurs.
17371741

1738-
To avoid this please take the following approach.
1742+
:meth:`~DataFrame.loc` tries to fit in what we are assigning to the current dtypes, while [ ] will overwrite them taking the dtype from the right hand side. Therefore the following piece of code produces the unintended result.
17391743

17401744
.. ipython:: python
17411745
17421746
df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]})
1743-
df[['a','b']] = df[['a','b']].astype(np.uint8)
1744-
df
1747+
df.loc[:, ['a', 'b']].astype(np.uint8).dtypes
1748+
df.loc[:, ['a', 'b']] = df.loc[:, ['a', 'b']].astype(np.uint8)
17451749
df.dtypes
17461750
17471751
object conversion

0 commit comments

Comments
 (0)