pyspark.sql.functions.unix_nanos#
- pyspark.sql.functions.unix_nanos(col)[source]#
Returns the number of nanoseconds since 1970-01-01 00:00:00 UTC as
DECIMAL(21, 0). Only supported forTIMESTAMP_LTZ(p)andTIMESTAMP_NTZ(p)with precisionpin[7, 9].Added in version 4.3.0.
- Parameters:
- col
Columnor column name input column of nanosecond-precision timestamp values to convert.
- col
- Returns:
Columnthe number of nanoseconds since 1970-01-01 00:00:00 UTC as
DECIMAL(21, 0).
See also
Examples
>>> import pyspark.sql.functions as sf >>> spark.conf.set("spark.sql.timestampNanosTypes.enabled", "true") >>> df = spark.sql( ... "SELECT TIMESTAMP_NTZ '2020-01-01 13:24:35.123456789' AS ts" ... ) >>> df.select('*', sf.unix_nanos('ts')).show(truncate=False) +-----------------------------+-------------------+ |ts |unix_nanos(ts) | +-----------------------------+-------------------+ |2020-01-01 13:24:35.123456789|1577885075123456789| +-----------------------------+-------------------+
>>> df.select(sf.unix_nanos(sf.lit(None).cast('timestamp_ntz(9)'))).show() +------------------------------------------+ |unix_nanos(CAST(NULL AS TIMESTAMP_NTZ(9)))| +------------------------------------------+ | NULL| +------------------------------------------+
>>> spark.conf.unset("spark.sql.timestampNanosTypes.enabled")