pyspark.sql.functions.try_mod#
- pyspark.sql.functions.try_mod(left, right)[source]#
Returns the remainder after dividend/divisor. Its result is always null if divisor is 0.
New in version 4.0.0.
Examples
Example 1: Integer divided by Integer.
>>> import pyspark.sql.functions as sf >>> spark.createDataFrame( ... [(6000, 15), (3, 2), (1234, 0)], ["a", "b"] ... ).select(sf.try_mod("a", "b")).show() +-------------+ |try_mod(a, b)| +-------------+ | 0| | 1| | NULL| +-------------+
Example 2: Exception during division, resulting in NULL when ANSI mode is on
>>> import pyspark.sql.functions as sf >>> origin = spark.conf.get("spark.sql.ansi.enabled") >>> spark.conf.set("spark.sql.ansi.enabled", "true") >>> try: ... df = spark.range(1) ... df.select(sf.try_mod(df.id, sf.lit(0))).show() ... finally: ... spark.conf.set("spark.sql.ansi.enabled", origin) +--------------+ |try_mod(id, 0)| +--------------+ | NULL| +--------------+