You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/indexing.rst
+22-18Lines changed: 22 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -34,16 +34,16 @@ indexing.
34
34
35
35
.. note::
36
36
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
39
39
across a wide range of use cases. This makes interactive work intuitive, as
40
40
there's little new to learn if you already know how to deal with Python
41
41
dictionaries and NumPy arrays. However, the type of the data to be accessed
42
42
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>`
47
47
48
48
See the :ref:`cookbook<cookbook.selection>` for some advanced strategies
49
49
@@ -522,17 +522,6 @@ indexing.advanced>` you may select along more than one axis using boolean
522
522
.. ipython:: python
523
523
524
524
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.
536
525
537
526
Where and Masking
538
527
~~~~~~~~~~~~~~~~~
@@ -542,7 +531,6 @@ subset of the data. To guarantee that selection output has the same shape as
542
531
the original data, you can use the ``where`` method in ``Series`` and ``
543
532
DataFrame``.
544
533
545
-
546
534
To return only the selected rows
547
535
548
536
.. ipython:: python
@@ -808,6 +796,8 @@ labels or even boolean vectors:
808
796
Slicing with labels is closely related to the ``truncate`` method which does
809
797
precisely ``.ix[start:stop]`` but returns a copy (for legacy reasons).
810
798
799
+
.. _indexing.view_versus_copy:
800
+
811
801
Returning a view versus a copy
812
802
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
813
803
@@ -817,6 +807,20 @@ indexing operation, the result will be a copy. With single label / scalar
817
807
indexing and slicing, e.g. ``df.ix[3:6]`` or ``df.ix[:, 'A']``, a view will be
818
808
returned.
819
809
810
+
In chained expressions, the order may determine whether a copy is returned or not:
0 commit comments