Skip to content

Commit 7269512

Browse files
authored
fix: replace function now can handle bpd.NA value. (#1786)
1 parent e3952e6 commit 7269512

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

bigframes/dtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ def bf_type_from_type_kind(
754754

755755
def is_dtype(scalar: typing.Any, dtype: Dtype) -> bool:
756756
"""Captures whether a scalar can be losslessly represented by a dtype."""
757-
if scalar is None:
757+
if pd.isna(scalar):
758758
return True
759759
if pd.api.types.is_bool_dtype(dtype):
760760
return pd.api.types.is_bool(scalar)

tests/system/small/test_series.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,18 @@ def test_series_replace_list_scalar(scalars_dfs):
629629
)
630630

631631

632+
def test_series_replace_nans_with_pd_na(scalars_dfs):
633+
scalars_df, scalars_pandas_df = scalars_dfs
634+
col_name = "string_col"
635+
bf_result = scalars_df[col_name].replace({pd.NA: "UNKNOWN"}).to_pandas()
636+
pd_result = scalars_pandas_df[col_name].replace({pd.NA: "UNKNOWN"})
637+
638+
pd.testing.assert_series_equal(
639+
pd_result,
640+
bf_result,
641+
)
642+
643+
632644
@pytest.mark.parametrize(
633645
("replacement_dict",),
634646
(

third_party/bigframes_vendored/ibis/expr/types/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import bigframes_vendored.ibis.expr.operations as ops
2020
from bigframes_vendored.ibis.expr.types.pretty import to_rich
2121
from bigframes_vendored.ibis.util import experimental
22+
import pandas as pd
2223
from public import public
2324
from rich.console import Console
2425
from rich.jupyter import JupyterMixin
@@ -34,7 +35,6 @@
3435
EdgeAttributeGetter,
3536
NodeAttributeGetter,
3637
)
37-
import pandas as pd
3838
import polars as pl
3939
import pyarrow as pa
4040
import torch
@@ -744,9 +744,9 @@ def _binop(op_class: type[ops.Binary], left: ir.Value, right: ir.Value) -> ir.Va
744744

745745
def _is_null_literal(value: Any) -> bool:
746746
"""Detect whether `value` will be treated by ibis as a null literal."""
747-
if value is None:
748-
return True
749747
if isinstance(value, Expr):
750748
op = value.op()
751749
return isinstance(op, ops.Literal) and op.value is None
750+
if pd.isna(value):
751+
return True
752752
return False

0 commit comments

Comments
 (0)