pyspark.sql.functions.unix_seconds#

pyspark.sql.functions.unix_seconds(col)[source]#

Returns the number of seconds since 1970-01-01 00:00:00 UTC. Truncates higher levels of precision.

Added in version 3.5.0.

Parameters:
colColumn or column name

input column of values to convert. A column that evaluates to a timestamp.

Returns:
Column

the number of seconds since 1970-01-01 00:00:00 UTC. Returns a column that evaluates to a long.

Examples

>>> spark.conf.set("spark.sql.session.timeZone", "America/Los_Angeles")
>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([('2015-07-22 10:00:00',), ('2022-10-09 11:12:13',)], ['ts'])
>>> df.select('*', sf.unix_seconds(sf.to_timestamp('ts'))).show()
+-------------------+------------------------------+
|                 ts|unix_seconds(to_timestamp(ts))|
+-------------------+------------------------------+
|2015-07-22 10:00:00|                    1437584400|
|2022-10-09 11:12:13|                    1665339133|
+-------------------+------------------------------+
>>> spark.conf.unset("spark.sql.session.timeZone")