Skip to content

Commit 0a4153c

Browse files
authored
fix: assign NaN scalar to column error. (#513)
* fix: assign NaN scalar to column error. * Update test. * remove import.
1 parent f6c40cd commit 0a4153c

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

bigframes/core/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ def assign_constant(
229229
value: typing.Any,
230230
dtype: typing.Optional[bigframes.dtypes.Dtype],
231231
) -> ArrayValue:
232+
if pandas.isna(value):
233+
# Need to assign a data type when value is NaN.
234+
dtype = dtype or bigframes.dtypes.DEFAULT_DTYPE
235+
232236
if destination_id in self.column_ids: # Mutate case
233237
exprs = [
234238
(

tests/system/small/test_dataframe.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -605,17 +605,24 @@ def test_assign_new_column_w_loc(scalars_dfs):
605605
pd.testing.assert_frame_equal(bf_result, pd_result)
606606

607607

608-
def test_assign_new_column_w_setitem(scalars_dfs):
608+
@pytest.mark.parametrize(
609+
("scalar",),
610+
[
611+
(2.1,),
612+
(None,),
613+
],
614+
)
615+
def test_assign_new_column_w_setitem(scalars_dfs, scalar):
609616
scalars_df, scalars_pandas_df = scalars_dfs
610617
bf_df = scalars_df.copy()
611618
pd_df = scalars_pandas_df.copy()
612-
bf_df["new_col"] = 2
613-
pd_df["new_col"] = 2
619+
bf_df["new_col"] = scalar
620+
pd_df["new_col"] = scalar
614621
bf_result = bf_df.to_pandas()
615622
pd_result = pd_df
616623

617-
# Convert default pandas dtypes `int64` to match BigQuery DataFrames dtypes.
618-
pd_result["new_col"] = pd_result["new_col"].astype("Int64")
624+
# Convert default pandas dtypes `float64` to match BigQuery DataFrames dtypes.
625+
pd_result["new_col"] = pd_result["new_col"].astype("Float64")
619626

620627
pd.testing.assert_frame_equal(bf_result, pd_result)
621628

0 commit comments

Comments
 (0)