pyspark.sql.functions.monthname#

pyspark.sql.functions.monthname(col)[source]#

Returns the three-letter abbreviated month name from the given date.

New in version 4.0.0.

Parameters
colColumn or column name

target date/timestamp column to work on.

Returns
Column

the three-letter abbreviation of month name for date/timestamp (Jan, Feb, Mar…)

Examples

Example 1: Extract the month name from a string column representing dates

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([('2015-04-08',), ('2024-10-31',)], ['dt'])
>>> df.select("*", sf.typeof('dt'), sf.monthname('dt')).show()
+----------+----------+-------------+
|        dt|typeof(dt)|monthname(dt)|
+----------+----------+-------------+
|2015-04-08|    string|          Apr|
|2024-10-31|    string|          Oct|
+----------+----------+-------------+

Example 2: Extract the month name from a string column representing timestamp

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([('2015-04-08 13:08:15',), ('2024-10-31 10:09:16',)], ['ts'])
>>> df.select("*", sf.typeof('ts'), sf.monthname('ts')).show()
+-------------------+----------+-------------+
|                 ts|typeof(ts)|monthname(ts)|
+-------------------+----------+-------------+
|2015-04-08 13:08:15|    string|          Apr|
|2024-10-31 10:09:16|    string|          Oct|
+-------------------+----------+-------------+

Example 3: Extract the month name from a date column

>>> import datetime
>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([
...     (datetime.date(2015, 4, 8),),
...     (datetime.date(2024, 10, 31),)], ['dt'])
>>> df.select("*", sf.typeof('dt'), sf.monthname('dt')).show()
+----------+----------+-------------+
|        dt|typeof(dt)|monthname(dt)|
+----------+----------+-------------+
|2015-04-08|      date|          Apr|
|2024-10-31|      date|          Oct|
+----------+----------+-------------+

Example 4: Extract the month name from a timestamp column

>>> import datetime
>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([
...     (datetime.datetime(2015, 4, 8, 13, 8, 15),),
...     (datetime.datetime(2024, 10, 31, 10, 9, 16),)], ['ts'])
>>> df.select("*", sf.typeof('ts'), sf.monthname('ts')).show()
+-------------------+----------+-------------+
|                 ts|typeof(ts)|monthname(ts)|
+-------------------+----------+-------------+
|2015-04-08 13:08:15| timestamp|          Apr|
|2024-10-31 10:09:16| timestamp|          Oct|
+-------------------+----------+-------------+