Skip to content

Commit edaac89

Browse files
authored
feat: support () operator between timedeltas (#1702)
1 parent 8f115e7 commit edaac89

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

bigframes/operations/numeric_ops.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,29 @@ def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionT
260260

261261
floordiv_op = FloorDivOp()
262262

263-
pow_op = base_ops.create_binary_op(name="pow", type_signature=op_typing.BINARY_NUMERIC)
264263

265-
mod_op = base_ops.create_binary_op(name="mod", type_signature=op_typing.BINARY_NUMERIC)
264+
@dataclasses.dataclass(frozen=True)
265+
class ModOp(base_ops.BinaryOp):
266+
name: typing.ClassVar[str] = "mod"
267+
268+
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
269+
left_type = input_types[0]
270+
right_type = input_types[1]
271+
272+
if left_type == dtypes.TIMEDELTA_DTYPE and right_type == dtypes.TIMEDELTA_DTYPE:
273+
return dtypes.TIMEDELTA_DTYPE
274+
275+
if (left_type is None or dtypes.is_numeric(left_type)) and (
276+
right_type is None or dtypes.is_numeric(right_type)
277+
):
278+
return dtypes.coerce_to_common(left_type, right_type)
279+
280+
raise TypeError(f"Cannot mod dtypes {left_type} and {right_type}")
281+
282+
283+
mod_op = ModOp()
284+
285+
pow_op = base_ops.create_binary_op(name="pow", type_signature=op_typing.BINARY_NUMERIC)
266286

267287
arctan2_op = base_ops.create_binary_op(
268288
name="arctan2", type_signature=op_typing.BINARY_REAL_NUMERIC

tests/system/small/operations/test_timedeltas.py

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def _assert_series_equal(actual: pd.Series, expected: pd.Series):
9898
(operator.floordiv, "timedelta_col_1", "float_col"),
9999
(operator.mul, "timedelta_col_1", "float_col"),
100100
(operator.mul, "float_col", "timedelta_col_1"),
101+
(operator.mod, "timedelta_col_1", "timedelta_col_2"),
101102
],
102103
)
103104
def test_timedelta_binary_ops_between_series(temporal_dfs, op, col_1, col_2):
@@ -120,6 +121,7 @@ def test_timedelta_binary_ops_between_series(temporal_dfs, op, col_1, col_2):
120121
(operator.floordiv, "timedelta_col_1", 3),
121122
(operator.mul, "timedelta_col_1", 3),
122123
(operator.mul, "float_col", pd.Timedelta(1, "s")),
124+
(operator.mod, "timedelta_col_1", pd.Timedelta(7, "s")),
123125
],
124126
)
125127
def test_timedelta_binary_ops_series_and_literal(temporal_dfs, op, col, literal):
@@ -142,6 +144,7 @@ def test_timedelta_binary_ops_series_and_literal(temporal_dfs, op, col, literal)
142144
(operator.floordiv, "float_col", pd.Timedelta(2, "s")),
143145
(operator.mul, "timedelta_col_1", 3),
144146
(operator.mul, "float_col", pd.Timedelta(1, "s")),
147+
(operator.mod, "timedelta_col_1", pd.Timedelta(7, "s")),
145148
],
146149
)
147150
def test_timedelta_binary_ops_literal_and_series(temporal_dfs, op, col, literal):

0 commit comments

Comments
 (0)