Skip to content

Commit 2d57979

Browse files
committed
updated docs for indexing to incorporate where and mask
1 parent 81169f9 commit 2d57979

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

doc/source/indexing.rst

+50-8
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,64 @@ Note, with the :ref:`advanced indexing <indexing.advanced>` ``ix`` method, you
231231
may select along more than one axis using boolean vectors combined with other
232232
indexing expressions.
233233

234-
Indexing a DataFrame with a boolean DataFrame
235-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234+
Where and Masking
235+
~~~~~~~~~~~~~~~~~
236236

237-
You may wish to set values on a DataFrame based on some boolean criteria
238-
derived from itself or another DataFrame or set of DataFrames. This can be done
239-
intuitively like so:
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).
240239

241240
.. ipython:: python
242241
242+
# return only the selected rows
243+
s[s > 0]
244+
245+
# return a Series of the same shape as the original
246+
s.where(s > 0)
247+
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.
250+
251+
.. ipython:: python
252+
253+
# return a DataFrame of the same shape as the original
254+
# this is equiavalent to `df.where(df < 0)`
255+
df[df < 0]
256+
257+
In addition, `where` takes an optional `other` argument for replacement of values where the
258+
condition is False, in the returned copy.
259+
260+
.. ipython:: python
261+
262+
df.where(df < 0, -df)
263+
264+
You may wish to set values based on some boolean criteria.
265+
This can be done intuitively like so:
266+
267+
.. ipython:: python
268+
269+
s2 = s.copy()
270+
s2[s2 < 0] = 0
271+
s2
272+
243273
df2 = df.copy()
244-
df2 < 0
245274
df2[df2 < 0] = 0
246275
df2
247276
248-
Note that such an operation requires that the boolean DataFrame is indexed
249-
exactly the same.
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)
279+
280+
.. ipython:: python
281+
282+
df2 = df.copy()
283+
df2[ df2[1:4] > 0 ] = 3
284+
df2
285+
286+
`mask` is the inverse boolean operation of `where`.
287+
288+
.. ipython:: python
289+
290+
s.mask(s >= 0)
291+
df.mask(df >= 0)
250292
251293
252294
Take Methods

0 commit comments

Comments
 (0)