Skip to content

Commit 18ab944

Browse files
committed
floordiv
1 parent 2695636 commit 18ab944

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

python/pyspark/pandas/data_type_ops/num_ops.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,14 @@ def floordiv(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex:
271271
_sanitize_list_like(right)
272272
if not is_valid_operand_for_numeric_arithmetic(right):
273273
raise TypeError("Floor division can not be applied to given types.")
274+
spark_session = left._internal.spark_frame.sparkSession
274275

275276
def floordiv(left: PySparkColumn, right: Any) -> PySparkColumn:
276277
return F.when(F.lit(right is np.nan), np.nan).otherwise(
277278
F.when(
278279
F.lit(right != 0) | F.lit(right).isNull(),
279280
F.floor(left.__div__(right)),
280-
).otherwise(F.lit(np.inf).__div__(left))
281+
).otherwise(F.try_divide(F.lit(np.inf), left))
281282
)
282283

283284
right = transform_boolean_operand_to_numeric(right, spark_type=left.spark.data_type)
@@ -369,6 +370,7 @@ def floordiv(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex:
369370
_sanitize_list_like(right)
370371
if not is_valid_operand_for_numeric_arithmetic(right):
371372
raise TypeError("Floor division can not be applied to given types.")
373+
spark_session = left._internal.spark_frame.sparkSession
372374

373375
def floordiv(left: PySparkColumn, right: Any) -> PySparkColumn:
374376
return F.when(F.lit(right is np.nan), np.nan).otherwise(
@@ -377,7 +379,7 @@ def floordiv(left: PySparkColumn, right: Any) -> PySparkColumn:
377379
F.floor(left.__div__(right)),
378380
).otherwise(
379381
F.when(F.lit(left == np.inf) | F.lit(left == -np.inf), left).otherwise(
380-
F.lit(np.inf).__div__(left)
382+
F.try_divide(F.lit(np.inf), left)
381383
)
382384
)
383385
)

python/pyspark/pandas/tests/computation/test_binary_ops.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ def test_binary_operator_truediv(self):
208208
self.assertRaisesRegex(TypeError, ks_err_msg, lambda: 1 / psdf["a"])
209209

210210
def test_binary_operator_floordiv(self):
211-
psdf = ps.DataFrame({"a": ["x"], "b": [1]})
211+
pdf = pd.DataFrame({"a": ["x"], "b": [1], "c": [1.0], "d": [0]})
212+
psdf = ps.from_pandas(pdf)
213+
self.assert_eq(pdf["b"] // 0, psdf["b"] // 0)
214+
self.assert_eq(pdf["c"] // 0, psdf["c"] // 0)
215+
self.assert_eq(pdf["d"] // 0, psdf["d"] // 0)
212216

213217
ks_err_msg = "Floor division can not be applied to strings"
214218
self.assertRaisesRegex(TypeError, ks_err_msg, lambda: psdf["a"] // psdf["b"])

0 commit comments

Comments
 (0)