Hello, I am seeking an elegant method to plot a timeline. In the provided code, I have the season for each day of a year. The plot should visually represent the duration of each season, indicating the start and end dates for each season.
import pandas as pd
from dateutil import parser
from dateutil.relativedelta import relativedeltaFunction to determine the season based on the date
def get_season(date):
month = date.month
if 3 <= month <= 5:
return ‘Spring’
elif 6 <= month <= 8:
return ‘Summer’
elif 9 <= month <= 11:
return ‘Autumn’
else:
return ‘Winter’Create a DataFrame with all dates for the year 2003
start_date = ‘2003-01-01’
end_date = ‘2003-12-31’
date_range = pd.date_range(start=start_date, end=end_date, freq=‘D’)Create a DataFrame with the dates and corresponding seasons
df = pd.DataFrame({‘Date’: date_range})
df[‘Season’] = df[‘Date’].apply(get_season)Print the resulting DataFrame
print(df)