Skip to content

Commit 8cb57aa

Browse files
fix: Fix clip int series with float bounds
1 parent 476b7dd commit 8cb57aa

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

bigframes/operations/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ def _align(
245245
)
246246
return (typing.cast(ex.DerefOp, values[0]), values[1], block)
247247

248-
def _align3(self, other1: series.Series | scalars.Scalar, other2: series.Series | scalars.Scalar, how="left") -> tuple[ex.DerefOp, AlignedExprT, AlignedExprT, blocks.Block]: # type: ignore
248+
def _align3(self, other1: series.Series | scalars.Scalar, other2: series.Series | scalars.Scalar, how="left", cast_scalars: bool = True) -> tuple[ex.DerefOp, AlignedExprT, AlignedExprT, blocks.Block]: # type: ignore
249249
"""Aligns the series value with 2 other scalars or series objects. Returns new values and joined tabled expression."""
250-
values, index = self._align_n([other1, other2], how)
250+
values, index = self._align_n([other1, other2], how, cast_scalars=cast_scalars)
251251
return (
252252
typing.cast(ex.DerefOp, values[0]),
253253
values[1],

bigframes/series.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,20 +1347,24 @@ def items(self):
13471347
yield item
13481348

13491349
def where(self, cond, other=None):
1350-
value_id, cond_id, other_id, block = self._align3(cond, other)
1350+
value_id, cond_id, other_id, block = self._align3(
1351+
cond, other, cast_scalars=False
1352+
)
13511353
block, result_id = block.project_expr(
13521354
ops.where_op.as_expr(value_id, cond_id, other_id)
13531355
)
13541356
return Series(block.select_column(result_id).with_column_labels([self.name]))
13551357

1356-
def clip(self, lower, upper):
1358+
def clip(self, lower=None, upper=None):
13571359
if lower is None and upper is None:
13581360
return self
13591361
if lower is None:
13601362
return self._apply_binary_op(upper, ops.minimum_op, alignment="left")
13611363
if upper is None:
13621364
return self._apply_binary_op(lower, ops.maximum_op, alignment="left")
1363-
value_id, lower_id, upper_id, block = self._align3(lower, upper)
1365+
value_id, lower_id, upper_id, block = self._align3(
1366+
lower, upper, cast_scalars=False
1367+
)
13641368
block, result_id = block.project_expr(
13651369
ops.clip_op.as_expr(value_id, lower_id, upper_id),
13661370
)

tests/system/small/test_series.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3001,6 +3001,17 @@ def test_clip(scalars_df_index, scalars_pandas_df_index, ordered):
30013001
assert_series_equal(bf_result, pd_result, ignore_order=not ordered)
30023002

30033003

3004+
def test_clip_int_with_float_bounds(scalars_df_index, scalars_pandas_df_index):
3005+
col_bf = scalars_df_index["int64_too"]
3006+
bf_result = col_bf.clip(-100, 3.14151593).to_pandas()
3007+
3008+
col_pd = scalars_pandas_df_index["int64_too"]
3009+
# pandas doesn't work with Int64 and clip with floats
3010+
pd_result = col_pd.astype("int64").clip(-100, 3.14151593).astype("Float64")
3011+
3012+
assert_series_equal(bf_result, pd_result)
3013+
3014+
30043015
def test_clip_filtered_two_sided(scalars_df_index, scalars_pandas_df_index):
30053016
col_bf = scalars_df_index["int64_col"].iloc[::2]
30063017
lower_bf = scalars_df_index["int64_too"].iloc[2:] - 1

0 commit comments

Comments
 (0)