Nóta
Teastaíonn údarú chun rochtain a fháil ar an leathanach seo. Is féidir leat triail a bhaint as shíniú isteach nó eolairí a athrú.
Teastaíonn údarú chun rochtain a fháil ar an leathanach seo. Is féidir leat triail a bhaint as eolairí a athrú.
Converts a column into TimeType using the optionally specified format. Specify formats according to datetime pattern. By default, it follows casting rules to TimeType if the format is omitted. Equivalent to col.cast("time").
Syntax
import pyspark.sql.functions as sf
sf.to_time(str=<str>)
# With format
sf.to_time(str=<str>, format=<format>)
Parameters
| Parameter | Type | Description |
|---|---|---|
str |
pyspark.sql.Column or str |
String to be parsed to time. |
format |
pyspark.sql.Column or str |
Optional. Time format pattern to follow. |
Returns
pyspark.sql.Column: time value as pyspark.sql.types.TimeType type.
Examples
Example 1: Convert string to a time.
import pyspark.sql.functions as sf
df = spark.createDataFrame([("10:30:00",)], ["str"])
df.select(sf.to_time(df.str)).show()
+------------+
|to_time(str)|
+------------+
| 10:30:00|
+------------+
Example 2: Convert string to a time with a format.
import pyspark.sql.functions as sf
df = spark.createDataFrame([("10:30:00", "HH:mm:ss")], ["str", "format"])
df.select(sf.to_time(df.str, df.format)).show()
+--------------------+
|to_time(str, format)|
+--------------------+
| 10:30:00|
+--------------------+