Skip to content

Commit 63561cf

Browse files
committed
fix
1 parent c4e3b4d commit 63561cf

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

python/pyspark/pandas/data_type_ops/num_ops.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,23 @@ 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:
276-
return F.when(F.lit(right is np.nan), np.nan).otherwise(
277-
F.when(
278-
F.lit(right != 0) | F.lit(right).isNull(),
279-
F.floor(left.__div__(right)),
280-
).otherwise(F.lit(np.inf).__div__(left))
281-
)
277+
if is_ansi_mode_enabled(spark_session):
278+
return F.when(F.lit(right is np.nan), np.nan).otherwise(
279+
F.when(
280+
F.lit(right != 0) | F.lit(right).isNull(),
281+
F.floor(left.__div__(right)),
282+
).otherwise(F.try_divide(F.lit(np.inf), left))
283+
)
284+
else:
285+
return F.when(F.lit(right is np.nan), np.nan).otherwise(
286+
F.when(
287+
F.lit(right != 0) | F.lit(right).isNull(),
288+
F.floor(left.__div__(right)),
289+
).otherwise(F.lit(np.inf).__div__(left))
290+
)
282291

283292
right = transform_boolean_operand_to_numeric(right, spark_type=left.spark.data_type)
284293
return numpy_column_op(floordiv)(left, right)
@@ -373,11 +382,14 @@ def floordiv(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex:
373382

374383
def floordiv(left: PySparkColumn, right: Any) -> PySparkColumn:
375384
if is_ansi_mode_enabled(spark_session):
376-
return F.when(F.lit(right is np.nan), F.lit(np.nan)).otherwise(
377-
F.when(F.lit(right != 0), F.floor(left / F.lit(right))).otherwise(
378-
F.when(
379-
F.lit(left == np.inf) | F.lit(left == -np.inf), F.lit(left)
380-
).otherwise(F.lit(np.inf).__div__(left))
385+
return F.when(F.lit(right is np.nan), np.nan).otherwise(
386+
F.when(
387+
F.lit(right != 0) | F.lit(right).isNull(),
388+
F.floor(left.__div__(right)),
389+
).otherwise(
390+
F.when(F.lit(left == np.inf) | F.lit(left == -np.inf), left).otherwise(
391+
F.try_divide(F.lit(np.inf), left)
392+
)
381393
)
382394
)
383395
else:

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,10 @@ 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-
pdf = pd.DataFrame({"a": ["x"], "b": [1]})
211+
pdf = pd.DataFrame({"a": ["x"], "b": [1], "c": [1.0]})
212212
psdf = ps.from_pandas(pdf)
213-
self.assert_eq(pdf["b"] / 0, psdf["b"] / 0)
213+
self.assert_eq(pdf["b"] // 0, psdf["b"] // 0)
214+
self.assert_eq(pdf["c"] // 0, psdf["c"] // 0)
214215

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

0 commit comments

Comments
 (0)