pyspark.pandas.Series.fillna#

Series.fillna(value=None, method=None, axis=None, inplace=False, limit=None)[source]#

Fill NA/NaN values.

Note

the current implementation of ‘method’ parameter in fillna uses Spark’s Window without specifying partition specification. This leads to moveing all data into a single partition in a single machine and could cause serious performance degradation. Avoid this method with very large datasets.

Parameters
valuescalar, dict, Series

Value to use to fill holes. alternately a dict/Series of values specifying which value to use for each column. DataFrame is not supported.

method{‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default None

Method to use for filling holes in reindexed Series pad / ffill: propagate last valid observation forward to next valid backfill / bfill: use NEXT valid observation to fill gap

Deprecated since version 4.0.0.

axis{0 or index}

1 and columns are not supported.

inplaceboolean, default False

Fill in place (do not create a new object)

limitint, default None

If method is specified, this is the maximum number of consecutive NaN values to forward/backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. Must be greater than 0 if not None

Deprecated since version 4.0.0.

Returns
Series

Series with NA entries filled.

Examples

>>> s = ps.Series([np.nan, 2, 3, 4, np.nan, 6], name='x')
>>> s
0    NaN
1    2.0
2    3.0
3    4.0
4    NaN
5    6.0
Name: x, dtype: float64

Replace all NaN elements with 0s.

>>> s.fillna(0)
0    0.0
1    2.0
2    3.0
3    4.0
4    0.0
5    6.0
Name: x, dtype: float64

We can also propagate non-null values forward or backward.

>>> s.fillna(method='ffill')
0    NaN
1    2.0
2    3.0
3    4.0
4    4.0
5    6.0
Name: x, dtype: float64
>>> s = ps.Series([np.nan, 'a', 'b', 'c', np.nan], name='x')
>>> s.fillna(method='ffill')
0    None
1       a
2       b
3       c
4       c
Name: x, dtype: object