Skip to content

Instantly share code, notes, and snippets.

@rsalaza4
Created January 17, 2025 15:31
Show Gist options
  • Select an option

  • Save rsalaza4/b48c81b29f690e48fa68b9cc9780fc79 to your computer and use it in GitHub Desktop.

Select an option

Save rsalaza4/b48c81b29f690e48fa68b9cc9780fc79 to your computer and use it in GitHub Desktop.
# Calculate timeframe from input file on a minute basis
# If the timespan from the source file is less than a day
if (pd.to_datetime(df.iloc[-1,0]) - pd.to_datetime(df.iloc[0,0])).days == 0:
timeframe = pd.date_range(
df.iloc[0,0],
periods=60*24,
freq='1min'
)
# If the timespan from the source file is more than a day
else:
timeframe = pd.date_range(
df.iloc[0,0],
periods=(pd.to_datetime(df.iloc[-1,0]) - pd.to_datetime(df.iloc[0,0])).days*60*24,
freq='1min'
)
# Create 'minutes' pandas data frame
minutes_df = pd.DataFrame({'Business Date':timeframe})
# Add Date column
minutes_df['Date'] = minutes_df['Business Date'].dt.date
# Add Time column
minutes_df['Time'] = minutes_df['Business Date'].dt.time
# Visualize top rows
minutes_df.head()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment