Skip to content

Commit de3492f

Browse files
committed
DOC: indexing.rst updates
1 parent 332c4ba commit de3492f

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

doc/source/10min.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ Selection
124124
.. note::
125125

126126
While standard Python / Numpy expressions for selecting and setting are
127-
intuitive and come handy for interactive work, for production code, we
128-
recommend the optimized pandas data access methods, ``.at``, ``.iat``,
127+
intuitive and come in handy for interactive work, for production code, we
128+
recommend the optimized pandas data access methods, ``.at``, ``.iat``,
129129
``.loc``, ``.iloc`` and ``.ix``.
130130

131131
See the :ref:`Indexing section <indexing>` and below.

doc/source/indexing.rst

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ indexing.
3434

3535
.. note::
3636

37-
Regular Python and NumPy indexing operators (squared brackets) and member
38-
operators (dots) provide quick and easy access to pandas data structures
37+
Regular Python and NumPy indexing operators ``[]`` and member
38+
operators (dots access) provide quick and easy access to pandas data structures
3939
across a wide range of use cases. This makes interactive work intuitive, as
4040
there's little new to learn if you already know how to deal with Python
4141
dictionaries and NumPy arrays. However, the type of the data to be accessed
4242
isn't known in advance. Therefore, accessing pandas objects directly using
43-
standard operators bears some optimization limits. In addition, whether a
44-
copy or a reference is returned here, may depend on context. For production
45-
code, we thus recommended to take advantage of the optimized pandas data
46-
access methods exposed in this chapter.
43+
standard operators has some optimization limits. In addition, whether a
44+
copy or a reference is returned for a selection operation, may depend on the context.
45+
For production code, we recommended that you take advantage of the optimized pandas data
46+
access methods exposed in this chapter. See :ref:`Returning View versus Copy <indexing.view_versus_copy>`
4747

4848
See the :ref:`cookbook<cookbook.selection>` for some advanced strategies
4949

@@ -522,17 +522,6 @@ indexing.advanced>` you may select along more than one axis using boolean
522522
.. ipython:: python
523523
524524
df2.loc[criterion & (df2['b'] == 'x'),'b':'c']
525-
526-
Caveat. Whether a copy or a reference is returned when using boolean indexing
527-
may depend on context, e.g., in chained expressions the order may determine
528-
whether a copy is returned or not:
529-
530-
.. ipython:: python
531-
532-
df2[df2.a.str.startswith('o')]['c'] = 42 # goes to copy (will be lost)
533-
df2['c'][df2.a.str.startswith('o')] = 42 # passed via reference (will stay)
534-
535-
When assigning values to subsets of your data, thus, make sure to either use the pandas access methods or explicitly handle the assignment creating a copy.
536525
537526
Where and Masking
538527
~~~~~~~~~~~~~~~~~
@@ -542,7 +531,6 @@ subset of the data. To guarantee that selection output has the same shape as
542531
the original data, you can use the ``where`` method in ``Series`` and ``
543532
DataFrame``.
544533

545-
546534
To return only the selected rows
547535

548536
.. ipython:: python
@@ -808,6 +796,8 @@ labels or even boolean vectors:
808796
Slicing with labels is closely related to the ``truncate`` method which does
809797
precisely ``.ix[start:stop]`` but returns a copy (for legacy reasons).
810798

799+
.. _indexing.view_versus_copy:
800+
811801
Returning a view versus a copy
812802
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
813803

@@ -817,6 +807,20 @@ indexing operation, the result will be a copy. With single label / scalar
817807
indexing and slicing, e.g. ``df.ix[3:6]`` or ``df.ix[:, 'A']``, a view will be
818808
returned.
819809

810+
In chained expressions, the order may determine whether a copy is returned or not:
811+
812+
.. ipython:: python
813+
814+
815+
dfb = DataFrame({'a' : ['one', 'one', 'two', 'three', 'two', 'one', 'six'],
816+
'b' : ['x', 'y', 'y', 'x', 'y', 'x', 'x'],
817+
'c' : randn(7)})
818+
dfb[dfb.a.str.startswith('o')]['c'] = 42 # goes to copy (will be lost)
819+
dfb['c'][dfb.a.str.startswith('o')] = 42 # passed via reference (will stay)
820+
821+
When assigning values to subsets of your data, thus, make sure to either use the
822+
pandas access methods or explicitly handle the assignment creating a copy.
823+
820824
The ``select`` method
821825
~~~~~~~~~~~~~~~~~~~~~
822826

0 commit comments

Comments
 (0)