Test your time-series wrangling skills by generating and querying a 365-day equipment maintenance log dataset. This challenge covers creating synthetic date ranges, slicing time intervals, filtering by year/month/weekday, calculating elapsed days, and performing temporal groupby() aggregations.
Run the script below to create a 365-day equipment maintenance log ending on today's date:
import datetime
import pandas as pd
import random
random.seed(42) # Reproducible seed
dt_range = 365 # 365 days of historical logs
dt_list = pd.date_range(end=datetime.date.today(), periods=dt_range).tolist()
l_activity = ["Checking", "Cleaning", "Inspection", "Repair", "Painting"]
l_dept = ["Dept1", "Dept2", "Dept3", "Dept4"]
l_eqpt = ["AC", "Fan", "Pump", "Generator", "Server", "Battery", "UPS"]
df = pd.DataFrame({
"Log_ID": [1000 + i for i in range(dt_range)],
"dt": pd.to_datetime(dt_list),
"dept": [random.choice(l_dept) for _ in range(dt_range)],
"activity": [random.choice(l_activity) for _ in range(dt_range)],
"eqpt": [random.choice(l_eqpt) for _ in range(dt_range)]
})
print(df.head(5))
Log_ID == 1012."Pump" was serviced."Pump" inspection relative to today?dt.weekday == 2).c_year = pd.to_datetime("now").year
count_year = len(df[df["dt"].dt.year == c_year])
print(f"Current Year Records: {count_year}")
march_records = df[df["dt"].dt.month == 3]
print(f"March Records: {len(march_records)}")
log_1012 = df.loc[df["Log_ID"] == 1012]
print(log_1012)
pump_logs = df[df["eqpt"] == "Pump"]
latest_pump = pump_logs["dt"].max()
days_elapsed = (pd.to_datetime("now") - latest_pump).days
print(f"Latest Pump Service: {latest_pump.strftime('%Y-%m-%d')}")
print(f"Days Elapsed: {days_elapsed} days")
cutoff_30d = pd.to_datetime("now") - pd.Timedelta(days=30)
recent_30d = df[df["dt"] >= cutoff_30d]
print(f"Past 30 Days Logs: {len(recent_30d)}")
wednesdays = df[df["dt"].dt.weekday == 2] # 0=Monday, 2=Wednesday
print(f"Wednesday Task Count: {len(wednesdays)}")
Aggregate counts across year, month, or combination intervals:
# Group by Year
print(df.groupby(df["dt"].dt.year)["Log_ID"].count())
# Group by Month
print(df.groupby(df["dt"].dt.month)["Log_ID"].count())
# Group by Year and Month combined
print(df.groupby([df["dt"].dt.year, df["dt"].dt.month])["Log_ID"].count())
Continue practicing across the complete 4-part datetime exercise sequence:
Open in Google Colab View on GitHub
Run all maintenance log queries, date range filters, and temporal group aggregations interactively in Google Colab. All synthetic generators are embedded in the notebook.
Reference: Pandas Time Series / Date functionality documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.