Created
May 13, 2022 12:56
-
-
Save vcabral19/e8df287710fb1aadf8a9ec3419f03949 to your computer and use it in GitHub Desktop.
some useful date functions (python + pyspark)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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