Skip to content

fix: replace function now can handle bpd.NA value. #1786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bigframes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ def bf_type_from_type_kind(

def is_dtype(scalar: typing.Any, dtype: Dtype) -> bool:
"""Captures whether a scalar can be losslessly represented by a dtype."""
if scalar is None:
if pd.isna(scalar):
return True
if pd.api.types.is_bool_dtype(dtype):
return pd.api.types.is_bool(scalar)
Expand Down
12 changes: 12 additions & 0 deletions tests/system/small/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,18 @@ def test_series_replace_list_scalar(scalars_dfs):
)


def test_series_replace_nans_with_pd_na(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
col_name = "string_col"
bf_result = scalars_df[col_name].replace({pd.NA: "UNKNOWN"}).to_pandas()
pd_result = scalars_pandas_df[col_name].replace({pd.NA: "UNKNOWN"})

pd.testing.assert_series_equal(
pd_result,
bf_result,
)


@pytest.mark.parametrize(
("replacement_dict",),
(
Expand Down
6 changes: 3 additions & 3 deletions third_party/bigframes_vendored/ibis/expr/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import bigframes_vendored.ibis.expr.operations as ops
from bigframes_vendored.ibis.expr.types.pretty import to_rich
from bigframes_vendored.ibis.util import experimental
import pandas as pd
from public import public
from rich.console import Console
from rich.jupyter import JupyterMixin
Expand All @@ -34,7 +35,6 @@
EdgeAttributeGetter,
NodeAttributeGetter,
)
import pandas as pd
import polars as pl
import pyarrow as pa
import torch
Expand Down Expand Up @@ -744,9 +744,9 @@ def _binop(op_class: type[ops.Binary], left: ir.Value, right: ir.Value) -> ir.Va

def _is_null_literal(value: Any) -> bool:
"""Detect whether `value` will be treated by ibis as a null literal."""
if value is None:
return True
if isinstance(value, Expr):
op = value.op()
return isinstance(op, ops.Literal) and op.value is None
if pd.isna(value):
return True
return False