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
So, why does this show the ``SettingWithCopy`` warning /and possibly not work when you do chained indexing and assignement:
1407
+
1408
+
.. code-block:: python
1409
+
1410
+
dfmi['one']['second'] = value
1411
+
1412
+
Since the chained indexing is2 calls, it is possible that either call may return a **copy** of the data because of the way it is sliced.
1413
+
Thus when setting, you are actually setting a **copy**, andnot the original frame data. It is impossible for pandas to figure this out because their are 2 separate python operations that are not connected.
1414
+
1415
+
The ``SettingWithCopy`` warning is a 'heuristic' to detect this (meaning it tends to catch most cases but is simply a lightweight check). Figuring this out for real is way complicated.
1416
+
1417
+
The ``.loc`` operation is a single python operation, and thus can select a slice (which still may be a copy), but allows pandas to assign that slice back into the frame after it is modified, thus setting the values as you would think.
1418
+
1419
+
The reason for having the ``SettingWithCopy`` warning is this. Sometimes when you slice an array you will simply get a view back, which means you can set it no problem. However, even a single dtyped array can generate a copy if it is sliced in a particular way. A multi-dtyped DataFrame (meaning it has say ``float``and``object`` data), will almost always yield a copy. Whether a view is created is dependent on the memory layout of the array.
1420
+
1421
+
Evaluation order matters
1422
+
~~~~~~~~~~~~~~~~~~~~~~~~
1423
+
1424
+
Furthermore, in chained expressions, the order may determine whether a copy is returned ornot.
1375
1425
If an expression will set values on a copy of a slice, then a ``SettingWithCopy``
1376
1426
exception will be raised (this raise/warn behavior is new starting in0.13.0)
0 commit comments