pyspark.pandas.Series.mask

Series.mask(cond: pyspark.pandas.series.Series, other: Any = nan) → pyspark.pandas.series.Series[source]

Replace values where the condition is True.

Parameters
condboolean Series

Where cond is False, keep the original value. Where True, replace with corresponding value from other.

otherscalar, Series

Entries where cond is True are replaced with corresponding value from other.

Returns
Series

Examples

>>> from pyspark.pandas.config import set_option, reset_option
>>> set_option("compute.ops_on_diff_frames", True)
>>> s1 = ps.Series([0, 1, 2, 3, 4])
>>> s2 = ps.Series([100, 200, 300, 400, 500])
>>> s1.mask(s1 > 0).sort_index()
0    0.0
1    NaN
2    NaN
3    NaN
4    NaN
dtype: float64
>>> s1.mask(s1 > 1, 10).sort_index()
0     0
1     1
2    10
3    10
4    10
dtype: int64
>>> s1.mask(s1 > 1, s1 + 100).sort_index()
0      0
1      1
2    102
3    103
4    104
dtype: int64
>>> s1.mask(s1 > 1, s2).sort_index()
0      0
1      1
2    300
3    400
4    500
dtype: int64
>>> reset_option("compute.ops_on_diff_frames")