try_make_timestamp
Try to create timestamp from years, months, days, hours, mins, secs and (optional) timezone fields. Alternatively, try to create timestamp from date, time, and (optional) timezone fields. The result data type is consistent with the value of configuration spark.sql.timestampType. The function returns NULL on invalid inputs.
Syntax
import pyspark.sql.functions as sf
# From individual components
sf.try_make_timestamp(years=<years>, months=<months>, days=<days>, hours=<hours>, mins=<mins>, secs=<secs>)
# With timezone
sf.try_make_timestamp(years=<years>, months=<months>, days=<days>, hours=<hours>, mins=<mins>, secs=<secs>, timezone=<timezone>)
# From date and time
sf.try_make_timestamp(date=<date>, time=<time>)
# From date and time with timezone
sf.try_make_timestamp(date=<date>, time=<time>, timezone=<timezone>)
Parameters
Parameter | Type | Description |
|---|---|---|
|
| The year to represent, from 1 to 9999. Required when creating timestamps from individual components. Must be used with months, days, hours, mins, and secs. |
|
| The month-of-year to represent, from 1 (January) to 12 (December). Required when creating timestamps from individual components. Must be used with years, days, hours, mins, and secs. |
|
| The day-of-month to represent, from 1 to 31. Required when creating timestamps from individual components. Must be used with years, months, hours, mins, and secs. |
|
| The hour-of-day to represent, from 0 to 23. Required when creating timestamps from individual components. Must be used with years, months, days, mins, and secs. |
|
| The minute-of-hour to represent, from 0 to 59. Required when creating timestamps from individual components. Must be used with years, months, days, hours, and secs. |
|
| The second-of-minute and its micro-fraction to represent, from 0 to 60. The value can be either an integer like 13, or a fraction like 13.123. If the sec argument equals to 60, the seconds field is set to 0 and 1 minute is added to the final timestamp. Required when creating timestamps from individual components. Must be used with years, months, days, hours, and mins. |
|
| Optional. The time zone identifier. For example, CET, UTC, and etc. |
|
| The date to represent, in valid DATE format. Required when creating timestamps from date and time components. Must be used with time parameter only. |
|
| The time to represent, in valid TIME format. Required when creating timestamps from date and time components. Must be used with date parameter only. |
Returns
pyspark.sql.Column: A new column that contains a timestamp or NULL in case of an error.
Examples
Example 1: Make timestamp from years, months, days, hours, mins, secs, and timezone.
import pyspark.sql.functions as sf
df = spark.createDataFrame([[2014, 12, 28, 6, 30, 45.887, 'CET']],
['year', 'month', 'day', 'hour', 'min', 'sec', 'tz'])
df.select(
sf.try_make_timestamp(df.year, df.month, df.day, 'hour', df.min, df.sec, 'tz')
).show(truncate=False)
+----------------------------------------------------+
|try_make_timestamp(year, month, day, hour, min, sec)|
+----------------------------------------------------+
|2014-12-27 21:30:45.887 |
+----------------------------------------------------+
Example 2: Make timestamp from years, months, days, hours, mins, and secs (without timezone).
import pyspark.sql.functions as sf
df = spark.createDataFrame([[2014, 12, 28, 6, 30, 45.887, 'CET']],
['year', 'month', 'day', 'hour', 'min', 'sec', 'tz'])
df.select(
sf.try_make_timestamp('year', 'month', df.day, df.hour, df.min, df.sec)
).show(truncate=False)
+------------------------------------------------------+
|try_make_timestamp(year, month, day, hour, min, sec) |
+------------------------------------------------------+
|2014-12-28 06:30:45.887 |
+------------------------------------------------------+
Example 3: Make timestamp from date and time with timezone.
import pyspark.sql.functions as sf
from datetime import date, time
df = spark.range(1).select(
sf.lit(date(2014, 12, 28)).alias("date"),
sf.lit(time(6, 30, 45, 887000)).alias("time"),
sf.lit("CET").alias("tz")
)
df.select(sf.try_make_timestamp(date=df.date, time=df.time, timezone=df.tz)).show(truncate=False)
+----------------------------------------+
|try_make_timestamp(date, time, timezone)|
+----------------------------------------+
|2014-12-27 21:30:45.887 |
+----------------------------------------+
Example 4: Make timestamp from date and time (without timezone).
import pyspark.sql.functions as sf
from datetime import date, time
df = spark.range(1).select(
sf.lit(date(2014, 12, 28)).alias("date"),
sf.lit(time(6, 30, 45, 887000)).alias("time")
)
df.select(sf.try_make_timestamp(date=df.date, time=df.time)).show(truncate=False)
+------------------------------+
|try_make_timestamp(date, time)|
+------------------------------+
|2014-12-28 06:30:45.887 |
+------------------------------+
Example 5: Returns NULL on invalid input.
import pyspark.sql.functions as sf
df = spark.createDataFrame([[2014, 13, 28, 6, 30, 45.887]],
['year', 'month', 'day', 'hour', 'min', 'sec'])
df.select(
sf.try_make_timestamp('year', 'month', df.day, df.hour, df.min, df.sec)
).show(truncate=False)
+------------------------------------------------------+
|try_make_timestamp(year, month, day, hour, min, sec) |
+------------------------------------------------------+
|NULL |
+------------------------------------------------------+