Skip to content

Instantly share code, notes, and snippets.

@vcabral19
Created May 13, 2022 12:56
Show Gist options
  • Save vcabral19/e8df287710fb1aadf8a9ec3419f03949 to your computer and use it in GitHub Desktop.
Save vcabral19/e8df287710fb1aadf8a9ec3419f03949 to your computer and use it in GitHub Desktop.
some useful date functions (python + pyspark)
from datetime import date, datetime
def _convert_date_string_to_epoch(date_string: str) -> Union[int, float]:
date_iso = date.fromisoformat(date_string)
return datetime.fromordinal(date_iso.toordinal()).timestamp()
def _convert_epoch_to_date_string(epoch_time: int) -> str:
date_ts = datetime.fromtimestamp(epoch_time)
return date_ts.strftime("%Y-%m-%d")
# This is pyspark
from pyspark.sql import Column
def _get_day_from_epoch_column(epoch_column: Column) -> Column:
return f.date_trunc(timestamp=f.to_timestamp(f.col(epoch_column) / 1000), format="day")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment