Skip to content

Instantly share code, notes, and snippets.

@FernandoBontorin
Last active June 12, 2024 14:50
Show Gist options
  • Select an option

  • Save FernandoBontorin/ee3cb70a2c0473c28c278b9e170355b0 to your computer and use it in GitHub Desktop.

Select an option

Save FernandoBontorin/ee3cb70a2c0473c28c278b9e170355b0 to your computer and use it in GitHub Desktop.
from pyspark.sql.functions import col, when, lit, last_day, add_months
def retro_add_months(date, months):
"""
Adds 'months' to the given 'date', but if the resulting date is not the last day of the month,
as well the given date, it returns the last day of the resulting month of added months.
"""
return when(last_day(date) != date, add_months(date, months)).otherwise(last_day(add_months(date, months)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment