Skip to content

Commit

Permalink
python: Series.fill_nan
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jan 16, 2022
1 parent 1a5a294 commit 913d102
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ Manipulation/ selection
Series.ceil
Series.set_at_idx
Series.fill_null
Series.fill_nan
Series.zip_with
Series.interpolate
Series.clip
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ def fill_null(self, fill_value: Union[int, float, bool, str, "Expr"]) -> "Expr":

def fill_nan(self, fill_value: Union[str, int, float, bool, "Expr"]) -> "Expr":
"""
Fill none value with a fill value
Fill floating point NaN value with a fill value
"""
fill_value = expr_to_lit_or_expr(fill_value, str_to_lit=True)
return wrap_expr(self._pyexpr.fill_nan(fill_value._pyexpr))
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3169,7 +3169,7 @@ def fill_null(self, strategy: Union[str, "pli.Expr", Any]) -> "DataFrame":

def fill_nan(self, fill_value: Union["pli.Expr", int, float]) -> "DataFrame":
"""
Fill None/missing values by a an Expression evaluation.
Fill floating point NaN values by an Expression evaluation.
Warnings
--------
Expand Down
10 changes: 10 additions & 0 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,16 @@ def __copy__(self) -> "Series":
def __deepcopy__(self, memodict: Any = {}) -> "Series":
return self.clone()

def fill_nan(
self, fill_value: Union[str, int, float, bool, "pli.Expr"]
) -> "Series":
"""
Fill floating point NaN value with a fill value
"""
return (
self.to_frame().select(pli.col(self.name).fill_nan(fill_value)).to_series()
)

def fill_null(self, strategy: Union[str, int, "pli.Expr"]) -> "Series":
"""
Fill null values with a filling strategy.
Expand Down
1 change: 1 addition & 0 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,7 @@ def test_fill_null() -> None:
def test_fill_nan() -> None:
df = pl.DataFrame({"a": [1, 2], "b": [3.0, float("nan")]})
assert df.fill_nan(4).frame_equal(pl.DataFrame({"a": [1, 2], "b": [3, 4]}))
assert df["b"].fill_nan(5.0).to_list() == [3.0, 5.0]


def test_shift_and_fill() -> None:
Expand Down

0 comments on commit 913d102

Please sign in to comment.