modin.pandas.DataFrame.mask¶

DataFrame.mask(cond: DataFrame | Series | Callable | AnyArrayLike, other: DataFrame | Series | Callable | Scalar | None = nan, *, inplace: bool = False, axis: Axis | None = None, level: Level | None = None)[source]¶

Replace values where the condition is True.

Parameters:
  • cond – bool Series/DataFrame, array-like, or callable Where cond is False, keep the original value. Where True, replace with corresponding value from other. If cond is callable, it is computed on the Series/DataFrame and should return boolean Series/DataFrame or array. The callable must not change input Series/DataFrame (though pandas doesn’t check it).

  • other – scalar, Series/DataFrame, or callable Entries where cond is True are replaced with corresponding value from other. If other is callable, it is computed on the Series/DataFrame and should return scalar or Series/DataFrame. The callable must not change input Series/DataFrame (though pandas doesn’t check it).

  • inplace – bool, default False Whether to perform the operation in place on the data.

  • axis – int, default None Alignment axis if needed. For Series this parameter is unused and defaults to 0.

  • level – int, default None Alignment level if needed.

Returns:

Same type as caller or None if inplace=True.

See also

DataFrame.where : Replace values where the condition is False.

Notes

The mask method is an application of the if-then idiom. For each element in the calling DataFrame, if cond is False the element is used; otherwise the corresponding element from the DataFrame other is used. If the axis of other does not align with axis of cond Series/DataFrame, the misaligned index positions will be filled with True.

The signature for DataFrame.where() differs from numpy.where(). Roughly df1.where(m, df2) is equivalent to np.where(m, df1, df2).

For further details and examples see the mask documentation in indexing.

The dtype of the object takes precedence. The fill value is cast to the object’s dtype, if this can be done losslessly.

Examples:: >>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=[‘A’, ‘B’]) >>> df # doctest: +NORMALIZE_WHITESPACE A B 0 0 1 1 2 3 2 4 5 3 6 7 4 8 9

>>> m = df % 3 == 0
>>> df.mask(m, -df)   
   A  B
0  0  1
1  2 -3
2  4  5
3 -6  7
4  8 -9
Copy

Snowpark pandas DataFrame.mask behaves the same as numpy.where.

>>> data = np.where(~m, df, -df)
>>> df.mask(m, -df) == pd.DataFrame(data, columns=['A', 'B'])  
    A     B
0  True  True
1  True  True
2  True  True
3  True  True
4  True  True
Copy
>>> df.mask(m, -df) == df.where(~m, -df)  
    A     B
0  True  True
1  True  True
2  True  True
3  True  True
4  True  True
Copy