Created
June 3, 2021 09:28
-
-
Save AyrtonB/cae232e185081d4f52699e1f79defcb5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import pandas as pd | |
def dt_rng_to_SPs(start_date:datetime, end_date:datetime, freq='30T', tz='Europe/London'): | |
dt_rng = pd.date_range(start_date, end_date, freq=freq, tz=tz) | |
dt_strs = dt_rng.strftime('%Y-%m-%d') | |
dt_SP_counts = pd.Series(dt_strs).groupby(dt_strs).count() | |
SPs = [] | |
for num_SPs in dt_SP_counts.values: | |
SPs += [SP+1 for SP in list(range(num_SPs))] | |
df_dates_SPs = pd.DataFrame({'date':dt_strs, 'SP':SPs}, index=dt_rng) | |
return df_dates_SPs | |
def assign_local_datetime(df, dt_col='settlementDate', SP_col='settlementPeriod', freq='30T', tz='Europe/London'): | |
start_date = df[dt_col].min() | |
end_date = df[dt_col].max() | |
start_date = (pd.to_datetime(start_date) - pd.Timedelta(days=1)).strftime('%Y-%m-%d') | |
end_date = (pd.to_datetime(end_date) + pd.Timedelta(days=1)).strftime('%Y-%m-%d') | |
ts_to_dt_SPs = dt_rng_to_SPs(start_date, end_date, freq=freq, tz=tz) | |
date_SP_tuples = list(zip(ts_to_dt_SPs['date'], ts_to_dt_SPs['SP'])) | |
dt_SP_to_ts = dict(zip(date_SP_tuples, ts_to_dt_SPs.index)) | |
s_dt_SPs = pd.Series(zip(df[dt_col], df[SP_col].astype(int)), index=df.index) | |
df = df.assign(datetime=s_dt_SPs.map(dt_SP_to_ts)) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment