pyspark.sql.functions.day#
- pyspark.sql.functions.day(col)[source]#
Extract the day of the month of a given date/timestamp as integer.
New in version 3.5.0.
- Parameters
- col
Column
or column name target date/timestamp column to work on.
- col
- Returns
Column
day of the month for given date/timestamp as integer.
See also
pyspark.sql.functions.year()
pyspark.sql.functions.quarter()
pyspark.sql.functions.month()
pyspark.sql.functions.hour()
pyspark.sql.functions.minute()
pyspark.sql.functions.second()
pyspark.sql.functions.dayname()
pyspark.sql.functions.dayofyear()
pyspark.sql.functions.dayofmonth()
pyspark.sql.functions.dayofweek()
pyspark.sql.functions.extract()
pyspark.sql.functions.datepart()
pyspark.sql.functions.date_part()
Examples
Example 1: Extract the day of the month 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.day('dt')).show() +----------+----------+-------+ | dt|typeof(dt)|day(dt)| +----------+----------+-------+ |2015-04-08| string| 8| |2024-10-31| string| 31| +----------+----------+-------+
Example 2: Extract the day of the month 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.day('ts')).show() +-------------------+----------+-------+ | ts|typeof(ts)|day(ts)| +-------------------+----------+-------+ |2015-04-08 13:08:15| string| 8| |2024-10-31 10:09:16| string| 31| +-------------------+----------+-------+
Example 3: Extract the day of the month 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.day('dt')).show() +----------+----------+-------+ | dt|typeof(dt)|day(dt)| +----------+----------+-------+ |2015-04-08| date| 8| |2024-10-31| date| 31| +----------+----------+-------+
Example 4: Extract the day of the month 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.day('ts')).show() +-------------------+----------+-------+ | ts|typeof(ts)|day(ts)| +-------------------+----------+-------+ |2015-04-08 13:08:15| timestamp| 8| |2024-10-31 10:09:16| timestamp| 31| +-------------------+----------+-------+