@@ -234,8 +234,9 @@ indexing expressions.
234
234
Where and Masking
235
235
~~~~~~~~~~~~~~~~~
236
236
237
- Selecting values from a Series with a boolean vector in the *[] *, returns a subset of the rows.
238
- The method `where ` allows selection that preserves the original data shape (and is a copy).
237
+ Selecting values from a Series with a boolean vector generally returns a subset of the data.
238
+ To guarantee that selection output has the same shape as the original data, you can use the
239
+ ``where `` method in ``Series `` and ``DataFrame ``.
239
240
240
241
.. ipython :: python
241
242
@@ -245,16 +246,16 @@ The method `where` allows selection that preserves the original data shape (and
245
246
# return a Series of the same shape as the original
246
247
s.where(s > 0 )
247
248
248
- Selecting values from a DataFrame with a boolean critierion in the * [] *, that is the same shape as
249
- the original DataFrame, returns a similary sized DataFrame (and is a copy). ` where ` is used under the hood as the implementation.
249
+ Selecting values from a DataFrame with a boolean critierion now also preserves input data shape.
250
+ `` where ` ` is used under the hood as the implementation.
250
251
251
252
.. ipython :: python
252
253
253
254
# return a DataFrame of the same shape as the original
254
- # this is equiavalent to `df.where(df < 0)`
255
+ # this is equiavalent to `` df.where(df < 0)``
255
256
df[df < 0 ]
256
257
257
- In addition, `where ` takes an optional `other ` argument for replacement of values where the
258
+ In addition, `` where `` takes an optional `` other `` argument for replacement of values where the
258
259
condition is False, in the returned copy.
259
260
260
261
.. ipython :: python
@@ -274,20 +275,32 @@ This can be done intuitively like so:
274
275
df2[df2 < 0 ] = 0
275
276
df2
276
277
277
- Furthermore, `where ` aligns the input boolean condition (ndarray or DataFrame), such that partial selection
278
- with setting is possible. This is analagous to partial setting via `.ix ` (but on the contents rather than the axis labels)
278
+ Furthermore, `` where ` ` aligns the input boolean condition (ndarray or DataFrame), such that partial selection
279
+ with setting is possible. This is analagous to partial setting via `` .ix ` ` (but on the contents rather than the axis labels)
279
280
280
281
.. ipython :: python
281
282
282
283
df2 = df.copy()
283
284
df2[ df2[1 :4 ] > 0 ] = 3
284
285
df2
285
286
286
- `mask ` is the inverse boolean operation of `where `.
287
+ By default, ``where `` returns a modified copy of the data. There is an optional parameter ``inplace ``
288
+ so that the original data can be modified without creating a copy:
289
+
290
+ .. ipython :: python
291
+
292
+ df_orig = df.copy()
293
+
294
+ df_orig.where(df > 0 , - df, inplace = True );
295
+
296
+ df_orig
297
+
298
+ ``mask `` is the inverse boolean operation of ``where ``.
287
299
288
300
.. ipython :: python
289
301
290
302
s.mask(s >= 0 )
303
+
291
304
df.mask(df >= 0 )
292
305
293
306
0 commit comments