Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion bigframes/core/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,15 @@ def __setitem__(
and isinstance(key[0], bigframes.series.Series)
and key[0].dtype == "boolean"
) and pd.api.types.is_scalar(value):
new_column = key[0].map({True: value, False: None})
# For integer scalar, if set value to a new column, the dtype would be default to float.
# But if set value to an existing Int64 column, the dtype would still be integer.
# So we need to use different NaN type to match this behavior.
new_column = key[0].map(
{
True: value,
False: pd.NA if key[1] in self._dataframe.columns else None,
}
)
try:
original_column = self._dataframe[key[1]]
except KeyError:
Expand Down
14 changes: 11 additions & 3 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2918,15 +2918,23 @@ def test_loc_setitem_bool_series_scalar_new_col(scalars_dfs):
)


def test_loc_setitem_bool_series_scalar_existing_col(scalars_dfs):
@pytest.mark.parametrize(
("col", "value"),
[
("string_col", "hello"),
("int64_col", 3),
("float64_col", 3.5),
],
)
def test_loc_setitem_bool_series_scalar_existing_col(scalars_dfs, col, value):
if pd.__version__.startswith("1."):
pytest.skip("this loc overload not supported in pandas 1.x.")

scalars_df, scalars_pandas_df = scalars_dfs
bf_df = scalars_df.copy()
pd_df = scalars_pandas_df.copy()
bf_df.loc[bf_df["int64_too"] == 1, "string_col"] = "hello"
pd_df.loc[pd_df["int64_too"] == 1, "string_col"] = "hello"
bf_df.loc[bf_df["int64_too"] == 1, col] = value
pd_df.loc[pd_df["int64_too"] == 1, col] = value

pd.testing.assert_frame_equal(
bf_df.to_pandas(),
Expand Down
6 changes: 3 additions & 3 deletions third_party/bigframes_vendored/pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,9 @@ def copy(self):

>>> df.loc[df["b"] == 2, "b"] = 22
>>> df
a b
0 1 22.0
1 3 4.0
a b
0 1 22
1 3 4
<BLANKLINE>
[2 rows x 2 columns]
>>> df_copy
Expand Down