def is_trading_date(entity_type, exchange, timestamp: pd.Timestamp): if type(timestamp) == str: timestamp = to_pd_timestamp(timestamp) # just ignore 00:00 # the_date = date_and_time(timestamp, '09:00') if entity_type == 'stock': return (timestamp.weekday() != 5) and (timestamp.weekday() != 6) return True
def generate_date_feature(date: pd.Timestamp): """ Generate date features. """ result = pd.Series() result['timestamp'] = date.timestamp() format_string = '%Y-%m-%d' dt_string = date.strftime(format_string) result['holiday'] = int( (dt_string in holiday) or (date.weekday() in [5, 6] and dt_string not in work)) result = pd.concat( [result, get_onehot(date.weekday(), 0, 6, name='weekday')]) return result
def test_non_nano_fields(self, dt64, ts): alt = Timestamp(dt64) assert ts.year == alt.year assert ts.month == alt.month assert ts.day == alt.day assert ts.hour == ts.minute == ts.second == ts.microsecond == 0 assert ts.nanosecond == 0 assert ts.to_julian_date() == alt.to_julian_date() assert ts.weekday() == alt.weekday() assert ts.isoweekday() == alt.isoweekday()
def get_time_of_use_period(dt: pd.Timestamp) -> str: """Return a string corresponding to the time-of-use category for the datetime.""" # 7PM - 7AM, weekends, and holidays are always off-peak if (dt.weekday() >= 5 or not (7 <= dt.hour < 12 + 7) or dt in ONTARIO_HOLIDAYS): return "Off-peak" # Nov - Apr is winter, versus May-Oct for summer is_winter = dt.month in (11, 12, 1, 2, 3, 4) # On-peak and off-peak switch between winter and summer if (7 <= dt.hour < 11) or (12 + 5 <= dt.hour < 12 + 7): return "On-peak" if is_winter else "Mid-peak" elif 11 <= dt.hour < 12 + 5: return "Mid-peak" if is_winter else "On-peak" else: assert False, "Time of use inference error." # should never occur!
def generate_requests(self, timestamp: pd.Timestamp): month = timestamp.month if timestamp.month in self.monthly_vals else 0 seconds_per_time_unit = self.__class__.SECONDS_IN_DAY // len( self.monthly_vals[month][timestamp.weekday()]) time_unit = (int(timestamp.timestamp()) % self.__class__.SECONDS_IN_DAY) // seconds_per_time_unit self._populate_split_across_seconds_if_needed(timestamp, month, seconds_per_time_unit, time_unit) second_in_time_unit = int(timestamp.timestamp( )) % seconds_per_time_unit # TODO: is it correct? self._populate_simulation_steps_in_second_if_needed( second_in_time_unit) return self._generate_requests_on_current_simulation_step(timestamp)
def _populate_split_across_seconds_if_needed(self, timestamp: pd.Timestamp, month: int, seconds_per_time_unit: int, time_unit: int): if month != self.current_month and time_unit != self.current_time_unit: # Generate the split if not available self.current_month = month self.current_time_unit = time_unit self.current_means_split_across_seconds = { s: 0 for s in range(seconds_per_time_unit) } avg_reqs_val = self.monthly_vals[month][ timestamp.weekday()][time_unit] for _ in range(avg_reqs_val): self.current_means_split_across_seconds[np.random.randint( seconds_per_time_unit)] += 1
def find_weekday(date_str): from pandas import Timestamp as ts date = ts.strptime(date_str, "%Y-%m-%d") return ts.weekday(date)