Last active
January 17, 2025 16:10
-
-
Save rsalaza4/1278d8e2392b7d8d0f514d203871236b 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
# Initialize date ranges list | |
date_ranges_list = [] | |
# Loop through all the orders placed on the source file | |
for i, row in df.iterrows(): | |
# Get Start/End Time | |
enter_t = row["OrderCreatedLocalDateTime"].replace(second=0, microsecond=0) | |
exit_t = row["TimeOutOfQueue"].replace(second=0, microsecond=0) | |
# Skip orders served within the same minute | |
if enter_t == exit_t: | |
continue | |
# Get the date ranges in minutes between Enter/Exit | |
date_range = pd.date_range( | |
start=enter_t, | |
end=exit_t, | |
freq="1min", | |
inclusive="right", | |
).tz_localize(None) | |
# Append the list of date ranges | |
date_ranges_list.append(pd.Series(date_range)) | |
# Accumulate all date ranges | |
all_date_ranges = pd.concat(date_ranges_list) | |
# Count the number of occurences for a date/time | |
orders_counts = all_date_ranges.value_counts().sort_index().to_frame(name="Orders") | |
# Merge the orders count into the minutes table | |
minutes_df = pd.merge( | |
minutes_df, | |
orders_counts, | |
how="left", | |
left_on="Business Date", | |
right_index=True | |
) | |
# Clear empty cells | |
minutes_df.fillna(0, inplace=True) | |
# Convert Orders column to int type | |
minutes_df['Orders'] = minutes_df['Orders'].astype('int') | |
# Visualize top rows | |
minutes_df.head() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment