pyspark.sql.functions.make_interval

pyspark.sql.functions.make_interval(years: Optional[ColumnOrName] = None, months: Optional[ColumnOrName] = None, weeks: Optional[ColumnOrName] = None, days: Optional[ColumnOrName] = None, hours: Optional[ColumnOrName] = None, mins: Optional[ColumnOrName] = None, secs: Optional[ColumnOrName] = None) → pyspark.sql.column.Column[source]

Make interval from years, months, weeks, days, hours, mins and secs.

New in version 3.5.0.

Parameters
yearsColumn or str

the number of years, positive or negative

monthsColumn or str

the number of months, positive or negative

weeksColumn or str

the number of weeks, positive or negative

daysColumn or str

the number of days, positive or negative

hoursColumn or str

the number of hours, positive or negative

minsColumn or str

the number of minutes, positive or negative

secsColumn or str

the number of seconds with the fractional part in microsecond precision.

Examples

>>> df = spark.createDataFrame([[100, 11, 1, 1, 12, 30, 01.001001]],
...     ["year", "month", "week", "day", "hour", "min", "sec"])
>>> df.select(make_interval(
...     df.year, df.month, df.week, df.day, df.hour, df.min, df.sec).alias('r')
... ).show(truncate=False)
+---------------------------------------------------------------+
|r                                                              |
+---------------------------------------------------------------+
|100 years 11 months 8 days 12 hours 30 minutes 1.001001 seconds|
+---------------------------------------------------------------+
>>> df.select(make_interval(
...     df.year, df.month, df.week, df.day, df.hour, df.min).alias('r')
... ).show(truncate=False)
+----------------------------------------------+
|r                                             |
+----------------------------------------------+
|100 years 11 months 8 days 12 hours 30 minutes|
+----------------------------------------------+
>>> df.select(make_interval(
...     df.year, df.month, df.week, df.day, df.hour).alias('r')
... ).show(truncate=False)
+-----------------------------------+
|r                                  |
+-----------------------------------+
|100 years 11 months 8 days 12 hours|
+-----------------------------------+
>>> df.select(make_interval(
...     df.year, df.month, df.week, df.day).alias('r')
... ).show(truncate=False)
+--------------------------+
|r                         |
+--------------------------+
|100 years 11 months 8 days|
+--------------------------+
>>> df.select(make_interval(
...     df.year, df.month, df.week).alias('r')
... ).show(truncate=False)
+--------------------------+
|r                         |
+--------------------------+
|100 years 11 months 7 days|
+--------------------------+
>>> df.select(make_interval(df.year, df.month).alias('r')).show(truncate=False)
+-------------------+
|r                  |
+-------------------+
|100 years 11 months|
+-------------------+
>>> df.select(make_interval(df.year).alias('r')).show(truncate=False)
+---------+
|r        |
+---------+
|100 years|
+---------+