54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
import pandas as pd
|
|
from datetime import datetime
|
|
import locale
|
|
|
|
file_path = 'examenrooster pre-syllabus.xlsx'
|
|
sheet_name = 'rooster'
|
|
|
|
df = pd.read_excel(file_path, sheet_name=sheet_name)
|
|
|
|
date_ranges = {
|
|
(pd.Timestamp('2025-05-26'), pd.Timestamp('2025-06-01')): 36,
|
|
(pd.Timestamp('2025-06-02'), pd.Timestamp('2025-06-08')): 37,
|
|
(pd.Timestamp('2025-06-09'), pd.Timestamp('2025-06-15')): 38,
|
|
(pd.Timestamp('2025-06-16'), pd.Timestamp('2025-06-22')): 39,
|
|
(pd.Timestamp('2025-08-18'), pd.Timestamp('2025-08-24')): 48,
|
|
(pd.Timestamp('2025-08-25'), pd.Timestamp('2025-08-31')): 49,
|
|
(pd.Timestamp('2025-09-01'), pd.Timestamp('2025-09-06')): 50,
|
|
}
|
|
|
|
def parse_custom_date(date_str):
|
|
if pd.isna(date_str):
|
|
return pd.NaT
|
|
if isinstance(date_str, pd.Timestamp):
|
|
return date_str
|
|
if isinstance(date_str, str):
|
|
try:
|
|
locale.setlocale(locale.LC_TIME, 'nl_NL.UTF-8')
|
|
return datetime.strptime(date_str, '%A %d %B %Y')
|
|
except ValueError as e:
|
|
raise ValueError(f"Date conversion error: {e} for date string: {date_str}")
|
|
finally:
|
|
locale.setlocale(locale.LC_TIME, 'C')
|
|
else:
|
|
raise TypeError(f"Expected string or Timestamp, got {type(date_str).__name__}: {date_str}")
|
|
|
|
def update_lesweek(date):
|
|
if pd.isna(date):
|
|
return 0
|
|
for date_range, lesweek_value in date_ranges.items():
|
|
if date_range[0] <= date <= date_range[1]:
|
|
return lesweek_value
|
|
return 0
|
|
|
|
if 'Datum' in df.columns:
|
|
try:
|
|
df['Datum'] = df['Datum'].apply(parse_custom_date)
|
|
print(df['Datum'].apply(type).value_counts()) # Debug: print types after parsing
|
|
except (ValueError, TypeError) as e:
|
|
print(f"Error: {e}")
|
|
raise
|
|
df['Lesweek'] = df['Datum'].apply(update_lesweek)
|
|
|
|
print("\nFirst few rows of the DataFrame to verify date formatting:\n", df.head())
|
|
df.to_excel('updated_rooster.xlsx', index=False) |