pyspark.sql.functions.make_ym_interval#

pyspark.sql.functions.make_ym_interval(years=None, months=None)[source]#

Make year-month interval from years, months.

New in version 3.5.0.

Parameters
yearsColumn or str, optional

The number of years, positive or negative

monthsColumn or str, optional

The number of months, positive or negative

Returns
Column

A new column that contains a year-month interval.

Examples

Example 1: Make year-month interval from years, months.

>>> import pyspark.sql.functions as sf
>>> spark.conf.set("spark.sql.session.timeZone", "America/Los_Angeles")
>>> df = spark.createDataFrame([[2014, 12]], ["year", "month"])
>>> df.select(sf.make_ym_interval(df.year, df.month)).show(truncate=False)
+-------------------------------+
|make_ym_interval(year, month)  |
+-------------------------------+
|INTERVAL '2015-0' YEAR TO MONTH|
+-------------------------------+

Example 2: Make year-month interval from years.

>>> import pyspark.sql.functions as sf
>>> spark.conf.set("spark.sql.session.timeZone", "America/Los_Angeles")
>>> df = spark.createDataFrame([[2014, 12]], ["year", "month"])
>>> df.select(sf.make_ym_interval(df.year)).show(truncate=False)
+-------------------------------+
|make_ym_interval(year, 0)      |
+-------------------------------+
|INTERVAL '2014-0' YEAR TO MONTH|
+-------------------------------+

Example 3: Make year-month interval.

>>> import pyspark.sql.functions as sf
>>> spark.conf.set("spark.sql.session.timeZone", "America/Los_Angeles")
>>> df = spark.createDataFrame([[2014, 12]], ["year", "month"])
>>> df.select(sf.make_ym_interval()).show(truncate=False)
+----------------------------+
|make_ym_interval(0, 0)      |
+----------------------------+
|INTERVAL '0-0' YEAR TO MONTH|
+----------------------------+
>>> spark.conf.unset("spark.sql.session.timeZone")