Skip to content

Instantly share code, notes, and snippets.

@realFranco
Created December 17, 2021 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realFranco/b3d21e946c1fb53644b7461a6a49f2c0 to your computer and use it in GitHub Desktop.
Save realFranco/b3d21e946c1fb53644b7461a6a49f2c0 to your computer and use it in GitHub Desktop.
Datetime Benchmark - Given a timestamp in seconds (epoch), return the day of the week
from math import ceil
import random
import pytz
from datetime import date
import pandas as pd
def dates_gen(cases: int = 1000) -> list:
return [random.randint(0, 1639758915) for _ in range(0, cases)]
# dates = [pd.Timestamp]
def get_week_of_month_vj(tss: list):
# la suma es como un gap, el día 1 del més es el día x de la semana en curso
dates = [pd.to_datetime(ts, unit='s') for ts in tss]
return [int(ceil((date.day + date.replace(day=1).day_of_week) / 7.0)) for date in dates]
# dates = [datetime.date]
def get_week_of_month_vf(tss: list) -> list:
gmt = pytz.timezone("GMT")
dates = [datetime.datetime.fromtimestamp(ts, gmt) for ts in tss]
return [int(ceil((date.day + date.replace(day=1).weekday()) / 7.0)) for date in dates]
date_cases = dates_gen(10)
pd_dates = get_week_of_month_vj(tss=date_cases)
dt_dates = get_week_of_month_vf(tss=date_cases)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment